|
| 1 | +import base64 |
| 2 | +from collections.abc import Callable |
| 3 | +from typing import cast |
| 4 | + |
| 5 | +from openai import AsyncOpenAI |
| 6 | +from openai.types.responses import Response |
| 7 | +from openai.types.responses.tool_param import CodeInterpreter, ToolParam |
| 8 | + |
| 9 | + |
| 10 | +def get_web_search_tool(model_name: str, additional_params: dict | None = None) -> Callable: |
| 11 | + """ |
| 12 | + Returns a native OpenAI web search tool as function |
| 13 | +
|
| 14 | + Args: |
| 15 | + model_name: The name of the model |
| 16 | + additional_params: The additional tool parameters |
| 17 | +
|
| 18 | + Returns: |
| 19 | + web search function |
| 20 | + """ |
| 21 | + params_to_pass = additional_params if additional_params else {} |
| 22 | + tool_object = OpenAITools(model_name, {"type": "web_search_preview", **params_to_pass}) |
| 23 | + return tool_object.search_web |
| 24 | + |
| 25 | + |
| 26 | +def get_code_interpreter_tool(model_name: str, additional_params: dict | None = None) -> Callable: |
| 27 | + """ |
| 28 | + Returns a native OpenAI code interpreter tool as function |
| 29 | +
|
| 30 | + Args: |
| 31 | + model_name: The name of the model |
| 32 | + additional_params: The additional tool parameters |
| 33 | +
|
| 34 | + Returns: |
| 35 | + code interpreter function |
| 36 | + """ |
| 37 | + params_to_pass = additional_params if additional_params else {} |
| 38 | + tool_object = OpenAITools(model_name, cast(CodeInterpreter, {"type": "code_interpreter", **params_to_pass})) |
| 39 | + return tool_object.code_interpreter |
| 40 | + |
| 41 | + |
| 42 | +def get_image_generation_tool(model_name: str, additional_params: dict | None = None) -> Callable: |
| 43 | + """ |
| 44 | + Returns a native OpenAI image generation tool as function |
| 45 | +
|
| 46 | + Args: |
| 47 | + model_name: The name of the model |
| 48 | + additional_params: The additional tool parameters |
| 49 | +
|
| 50 | + Returns: |
| 51 | + image generation function |
| 52 | + """ |
| 53 | + params_to_pass = additional_params if additional_params else {} |
| 54 | + tool_object = OpenAITools(model_name, {"type": "image_generation", **params_to_pass}) |
| 55 | + return tool_object.image_generation |
| 56 | + |
| 57 | + |
| 58 | +class OpenAIResponsesLLM: |
| 59 | + """ |
| 60 | + Class serving as a wrapper for tool calls to responses API of OpenAI |
| 61 | + """ |
| 62 | + |
| 63 | + def __init__(self, model_name: str, tool_param: ToolParam): |
| 64 | + self._client = AsyncOpenAI() |
| 65 | + self._model_name = model_name |
| 66 | + self._tool_param = tool_param |
| 67 | + |
| 68 | + async def use_tool(self, query: str) -> Response: |
| 69 | + """ |
| 70 | + Uses tool based on query and returns output. |
| 71 | +
|
| 72 | + Args: |
| 73 | + query: query for the tool |
| 74 | +
|
| 75 | + Returns: |
| 76 | + Output of the tool |
| 77 | + """ |
| 78 | + return await self._client.responses.create( |
| 79 | + model=self._model_name, |
| 80 | + tools=[self._tool_param], |
| 81 | + tool_choice="required", |
| 82 | + input=query, |
| 83 | + ) |
| 84 | + |
| 85 | + |
| 86 | +class OpenAITools: |
| 87 | + """ |
| 88 | + Class wrapping tool calls to responses API of OpenAI |
| 89 | + """ |
| 90 | + |
| 91 | + AVAILABLE_TOOLS = {"web_search_preview", "code_interpreter", "image_generation"} |
| 92 | + |
| 93 | + def __init__(self, model_name: str, tool_param: ToolParam): |
| 94 | + self._responses_llm = OpenAIResponsesLLM(model_name, tool_param) |
| 95 | + |
| 96 | + async def search_web(self, query: str) -> str: |
| 97 | + """ |
| 98 | + Searches web for a query |
| 99 | +
|
| 100 | + Args: |
| 101 | + query: The query to search |
| 102 | +
|
| 103 | + Returns: |
| 104 | + The web search result |
| 105 | + """ |
| 106 | + return (await self._responses_llm.use_tool(query)).output_text |
| 107 | + |
| 108 | + async def code_interpreter(self, query: str) -> str: |
| 109 | + """ |
| 110 | + Performs actions in code interpreter based on query |
| 111 | +
|
| 112 | + Args: |
| 113 | + query: The query with instructions |
| 114 | +
|
| 115 | + Returns: |
| 116 | + Output of the interpreter |
| 117 | + """ |
| 118 | + return (await self._responses_llm.use_tool(query)).output_text |
| 119 | + |
| 120 | + async def image_generation(self, query: str, save_path: str = "generated_image.png") -> str: |
| 121 | + """ |
| 122 | + Generate image based on query. |
| 123 | +
|
| 124 | + Args: |
| 125 | + query: The query with instructions |
| 126 | + save_path: The path to save the generated image |
| 127 | +
|
| 128 | + Returns: |
| 129 | + LLM text output |
| 130 | + """ |
| 131 | + response = await self._responses_llm.use_tool(query) |
| 132 | + image_data = next((output.result for output in response.output if output.type == "image_generation_call"), None) |
| 133 | + |
| 134 | + if image_data: |
| 135 | + with open(save_path, "wb") as f: |
| 136 | + f.write(base64.b64decode(image_data)) |
| 137 | + text_prefix = f"Image saved to {save_path}\n" |
| 138 | + else: |
| 139 | + text_prefix = "No generated image was returned\n" |
| 140 | + |
| 141 | + return text_prefix + response.output_text |
0 commit comments