assistant = client.beta.assistants.create( name="Math Tutor", instructions="You are a personal math tutor. Answer questions briefly, in a sentence or less.", model="gpt-4-1106-preview", ) show_json(assistant)
{'id': 'asst_9HAjl9y41ufsViNcThW1EXUS', 'created_at': 1699828331, 'description': None, 'file_ids': [], 'instructions': 'You are a personal math tutor. Answer questions briefly, in a sentence or less.', 'metadata': {}, 'model': 'gpt-4-1106-preview', 'name': 'Math Tutor', 'object': 'assistant', 'tools': []}
message = client.beta.threads.messages.create( thread_id=thread.id, role="user", content="I need to solve the equation `3x + 11 = 14`. Can you help me?", ) show_json(message)
#立刻返回的结果是用户的问题的消息,待会运行后,助手的消息也是在message中存在。 {'id': 'msg_IBiZDAWHhWPewxzN0EfTYNew', 'assistant_id': None, 'content': [{'text': {'annotations': [], 'value': 'I need to solve the equation `3x + 11 = 14`. Can you help me?'}, 'type': 'text'}], 'created_at': 1699828332, 'file_ids': [], 'metadata': {}, 'object': 'thread.message', 'role': 'user', 'run_id': None, 'thread_id': 'thread_bw42vPoQtYBMQE84WubNcJXG'}
def wait_on_run(run, thread): while run.status == "queued" or run.status == "in_progress": run = client.beta.threads.runs.retrieve( thread_id=thread.id, run_id=run.id, ) time.sleep(0.5) return run run = wait_on_run(run, thread) show_json(run)
{'id': 'run_LA08RjouV3RemQ78UZXuyzv6', 'assistant_id': 'asst_9HAjl9y41ufsViNcThW1EXUS', 'cancelled_at': None, 'completed_at': 1699828333, 'created_at': 1699828332, 'expires_at': None, 'failed_at': None, 'file_ids': [], 'instructions': 'You are a personal math tutor. Answer questions briefly, in a sentence or less.', 'last_error': None, 'metadata': {}, 'model': 'gpt-4-1106-preview', 'object': 'thread.run', 'required_action': None, 'started_at': 1699828332, 'status': 'completed', 'thread_id': 'thread_bw42vPoQtYBMQE84WubNcJXG', 'tools': []}
{'data': [{'id': 'msg_S0ZtKIWjyWtbIW9JNUocPdUS', 'assistant_id': 'asst_9HAjl9y41ufsViNcThW1EXUS', 'content': [{'text': {'annotations': [], 'value': 'Yes. Subtract 11 from both sides to get `3x = 3`, then divide by 3 to find `x = 1`.'}, 'type': 'text'}], 'created_at': 1699828333, 'file_ids': [], 'metadata': {}, 'object': 'thread.message', 'role': 'assistant', 'run_id': 'run_LA08RjouV3RemQ78UZXuyzv6', 'thread_id': 'thread_bw42vPoQtYBMQE84WubNcJXG'}, {'id': 'msg_IBiZDAWHhWPewxzN0EfTYNew', 'assistant_id': None, 'content': [{'text': {'annotations': [], 'value': 'I need to solve the equation `3x + 11 = 14`. Can you help me?'}, 'type': 'text'}], 'created_at': 1699828332, 'file_ids': [], 'metadata': {}, 'object': 'thread.message', 'role': 'user', 'run_id': None, 'thread_id': 'thread_bw42vPoQtYBMQE84WubNcJXG'}], 'object': 'list', 'first_id': 'msg_S0ZtKIWjyWtbIW9JNUocPdUS', 'last_id': 'msg_IBiZDAWHhWPewxzN0EfTYNew', 'has_more': False}
6)用户问下一个问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# Create a message to append to our thread message = client.beta.threads.messages.create( thread_id=thread.id, role="user", content="Could you explain this to me?" )
# Execute our run run = client.beta.threads.runs.create( thread_id=thread.id, assistant_id=assistant.id, )
# Wait for completion wait_on_run(run, thread)
# Retrieve all the messages added after our last user message messages = client.beta.threads.messages.list( thread_id=thread.id, order="asc", after=message.id ) show_json(messages)