|
| 1 | +import json |
| 2 | + |
| 3 | +from openai import AssistantEventHandler |
| 4 | + |
| 5 | + |
| 6 | +def test_sub_stream_with_submit_tool_outputs_stream(client): |
| 7 | + |
| 8 | + def get_current_weather(location): |
| 9 | + return f"{location}今天是雨天。 " |
| 10 | + |
| 11 | + assistant = client.beta.assistants.create( |
| 12 | + name="Assistant Demo", |
| 13 | + instructions="You are a helpful assistant. When asked a question, use tools wherever possible.", |
| 14 | + model="gpt-4o", |
| 15 | + tools=[ |
| 16 | + { |
| 17 | + "type": "function", |
| 18 | + "function": { |
| 19 | + "name": "get_current_weather", |
| 20 | + "description": "当你想查询指定城市的天气时非常有用。", |
| 21 | + "parameters": {"type": "object", "properties": {"location": {"type": "string", "description": "城市或县区,比如北京市、杭州市、余杭区等。"}}, "required": ["location"]}, # 查询天气时需要提供位置,因此参数设置为location |
| 22 | + }, |
| 23 | + } |
| 24 | + ], |
| 25 | + ) |
| 26 | + print("=====> : %s\n", assistant) |
| 27 | + |
| 28 | + thread = client.beta.threads.create() |
| 29 | + print("=====> : %s\n", thread) |
| 30 | + |
| 31 | + message = client.beta.threads.messages.create( |
| 32 | + thread_id=thread.id, |
| 33 | + role="user", |
| 34 | + content="北京天气如何?", |
| 35 | + ) |
| 36 | + print("=====> : %s\n", message) |
| 37 | + |
| 38 | + funcs = [get_current_weather] |
| 39 | + |
| 40 | + class EventHandler(AssistantEventHandler): |
| 41 | + |
| 42 | + def on_event(self, event): |
| 43 | + print(event.event) |
| 44 | + if event.event == "thread.run.requires_action": |
| 45 | + print(event) |
| 46 | + run_id = event.data.id # Retrieve the run ID from the event data |
| 47 | + self.handle_requires_action(event.data, run_id) |
| 48 | + |
| 49 | + def handle_requires_action(self, data, run_id): |
| 50 | + tool_outputs = [] |
| 51 | + |
| 52 | + for tool in data.required_action.submit_tool_outputs.tool_calls: |
| 53 | + func = next(iter([func for func in funcs if func.__name__ == tool.function.name])) |
| 54 | + try: |
| 55 | + output = func(**eval(tool.function.arguments)) |
| 56 | + except Exception as e: |
| 57 | + output = "Error: " + str(e) |
| 58 | + |
| 59 | + tool_outputs.append({"tool_call_id": tool.id, "output": json.dumps(output)}) |
| 60 | + |
| 61 | + print(tool_outputs) |
| 62 | + |
| 63 | + # Submit all tool_outputs at the same time |
| 64 | + self.submit_tool_outputs(tool_outputs, run_id) |
| 65 | + |
| 66 | + def submit_tool_outputs(self, tool_outputs, run_id): |
| 67 | + # Use the submit_tool_outputs_stream helper |
| 68 | + with client.beta.threads.runs.submit_tool_outputs_stream( |
| 69 | + thread_id=self.current_run.thread_id, |
| 70 | + run_id=self.current_run.id, |
| 71 | + tool_outputs=tool_outputs, |
| 72 | + event_handler=EventHandler(), |
| 73 | + ) as stream: |
| 74 | + # for text in stream.text_deltas: |
| 75 | + # print(text, end="", flush=True) |
| 76 | + # print() |
| 77 | + stream.until_done() |
| 78 | + |
| 79 | + def on_text_delta(self, delta, snapshot) -> None: |
| 80 | + print("=====> text delta") |
| 81 | + print("delta : %s", delta) |
| 82 | + |
| 83 | + with client.beta.threads.runs.stream( |
| 84 | + thread_id=thread.id, |
| 85 | + assistant_id=assistant.id, |
| 86 | + event_handler=EventHandler(), |
| 87 | + ) as stream: |
| 88 | + stream.until_done() |
0 commit comments