From bdc483993db58b42cb5940860f2b7339bc10c4d8 Mon Sep 17 00:00:00 2001 From: Jonathan Lucas Date: Mon, 28 Oct 2024 17:56:06 -0500 Subject: [PATCH] Added standalone llm --- tools/standalone_llm_tool.py | 58 ++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 tools/standalone_llm_tool.py diff --git a/tools/standalone_llm_tool.py b/tools/standalone_llm_tool.py new file mode 100644 index 0000000..1b712ce --- /dev/null +++ b/tools/standalone_llm_tool.py @@ -0,0 +1,58 @@ +from .base_tool import BaseTool +import os +import json +import logging +from openai import OpenAI + +class StandaloneLLMTool(BaseTool): + def __init__(self): + self.client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY")) + + def clear(self): + pass + + def get_functions(self): + return [ + { + "name": "call_external_llm", + "description": "Call an external language model", + "parameters": { + "type": "object", + "properties": { + "prompt": { + "type": "string", + "description": "The prompt you are providing" + }, + "model": { + "type": "string", + "description": "The model to use for generating the detailed instructions. Use mini for most coding tasks, preview when needing sophisticated reasoning", + "enum": ["o1-mini", "o1-preview"], + "default": "o1-mini" + }, + "max_tokens": { + "type": "integer", + "description": "The maximum number of tokens to use for generating the detailed instructions. Default is 16384.", + } + }, + "required": ["prompt"] + } + } + ] + + def execute(self, function_name, **kwargs): + if function_name == "call_external_llm": + return self.call_external_llm(kwargs.get("prompt"), kwargs.get("model"), kwargs.get("max_tokens")) + else: + error_message = f"Unknown function: {function_name}" + logging.error(error_message) + + def call_external_llm(self, prompt, model="o1-mini", max_tokens=16384): + logging.info(f"Calling external model: {model}") + response = self.client.completions.create( + model=model, + prompt=prompt, + max_tokens=max_tokens + ) + token_amount = response.summary["total_tokens"] + logging.info("Response generated, {token_amount} tokens used.") + return response.choices[0].text \ No newline at end of file