diff --git a/standalone_llm_tool.py b/standalone_llm_tool.py new file mode 100644 index 0000000..b4305e6 --- /dev/null +++ b/standalone_llm_tool.py @@ -0,0 +1,29 @@ +import os +import json +import logging +from openai import OpenAI + +class StandaloneLLMTool: + def __init__(self): + self.client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY")) + + def get_detailed_instructions(self, user_prompt, model="llm-preview", max_tokens=16384): + response = self.client.completions.create( + model=model, + prompt=user_prompt, + max_tokens=max_tokens + ) + return response + + def process_user_input(self, user_prompt, model="llm-preview", max_tokens=16384): + logging.info(f"Received prompt: {user_prompt}") + response = self.get_detailed_instructions(user_prompt, model, max_tokens) + logging.info("Response generated") + return response.choices[0].text + + +# Utility function for programmatic access + +def get_llm_response(prompt, model="llm-preview", max_tokens=16384): + tool = StandaloneLLMTool() + return tool.process_user_input(prompt, model, max_tokens) diff --git a/tools/standalone_llm_tool.md b/tools/standalone_llm_tool.md new file mode 100644 index 0000000..6c4d2b7 --- /dev/null +++ b/tools/standalone_llm_tool.md @@ -0,0 +1,38 @@ +# Standalone LLM Tool + +## Overview +The Standalone LLM Tool is designed to interact with a preview version of a Large Language Model (LLM) programmatically. This tool utilizes advanced reasoning and coding capabilities to generate responses based on user input prompts. + +## Setup +1. **Environment Variables**: Ensure that the `OPENAI_API_KEY` is set in your environment to authenticate API requests. +2. **Dependencies**: Make sure all dependencies are installed as per `requirements.txt`. + +## Usage +Instead of using command-line prompts, this tool can now be integrated directly into your Python projects: + +### Function Usage +- Import the tool and use the following utility function: + ```python + from standalone_llm_tool import get_llm_response + + # Parameters: prompt (str), model (str, optional), max_tokens (int, optional) + response = get_llm_response("What is AI?", model="llm-preview", max_tokens=16384) + + print(response) + ``` + +## Features +- **LLM Model**: Accepts a designated model parameter for flexible processing. +- **Prompt Handling**: Accepts user input and provides comprehensive instructions or code snippets. +- **Dynamic Parameters**: Allows customization of the model and max tokens per request. +- **Advanced Reasoning**: Leverages the LLM's capabilities for enhanced reasoning and coding tasks. + +## Notes +- The model and token parameters are dynamically handled, offering flexibility for various application needs. +- This tool is ideal for generating detailed narratives or solving coding-related queries due to its advanced LLM capabilities. + +## Troubleshooting +For any issues encountered while using the tool, consider the following: +- Verify API key validity and quota. +- Ensure your Python environment is correctly set up with necessary dependencies. +- Refer to any console logs for specific error messages to aid in debugging. 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