OpenAIProvider class definition
| 28 | |
| 29 | |
| 30 | class OpenAIProvider( |
| 31 | NonAgenticProvider[OpenAITool, OpenAIToolCollection], name="openai" |
| 32 | ): |
| 33 | """OpenAIProvider class definition""" |
| 34 | |
| 35 | def wrap_tool(self, tool: Tool) -> OpenAITool: |
| 36 | return ChatCompletionToolParam( |
| 37 | function=FunctionDefinition( |
| 38 | name=tool.slug, |
| 39 | description=tool.description, |
| 40 | parameters=t.cast(FunctionParameters, tool.input_parameters), |
| 41 | strict=None, |
| 42 | ), |
| 43 | type="function", |
| 44 | ) |
| 45 | |
| 46 | def wrap_tools(self, tools: t.Sequence[Tool]) -> OpenAIToolCollection: |
| 47 | return [self.wrap_tool(tool) for tool in tools] |
| 48 | |
| 49 | def execute_tool_call( |
| 50 | self, |
| 51 | user_id: str, |
| 52 | tool_call: ChatCompletionMessageToolCall, |
| 53 | modifiers: t.Optional[Modifiers] = None, |
| 54 | ) -> ToolExecutionResponse: |
| 55 | """Execute a tool call. |
| 56 | |
| 57 | :param tool_call: Tool call metadata. |
| 58 | :param user_id: User ID to use for executing the function call. |
| 59 | :return: Object containing output data from the tool call. |
| 60 | """ |
| 61 | # OpenAI always serializes tool arguments as a JSON string; normalize |
| 62 | # tolerates empty / object-shaped payloads too (issue #2406). |
| 63 | return self.execute_tool( |
| 64 | slug=tool_call.function.name, |
| 65 | arguments=normalize_tool_arguments(tool_call.function.arguments), |
| 66 | modifiers=modifiers, |
| 67 | user_id=user_id, |
| 68 | ) |
| 69 | |
| 70 | def handle_tool_calls( |
| 71 | self, |
| 72 | user_id: str, |
| 73 | response: ChatCompletion, |
| 74 | modifiers: t.Optional[Modifiers] = None, |
| 75 | ) -> t.List[ToolExecutionResponse]: |
| 76 | """ |
| 77 | Handle tool calls from OpenAI chat completion object. |
| 78 | |
| 79 | :param response: Chat completion object from |
| 80 | openai.OpenAI.chat.completions.create function call |
| 81 | :param user_id: User ID to use for executing the function call. |
| 82 | :return: A list of output objects from the function calls. |
| 83 | """ |
| 84 | outputs = [] |
| 85 | for choice in response.choices: |
| 86 | if choice.message.tool_calls is None: |
| 87 | continue |
no outgoing calls
searching dependent graphs…