2024-10-28 17:56:06 -05:00
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 [
{
2025-06-02 13:23:02 -05:00
" type " : " function " ,
" function " : {
" 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 " ,
2025-06-03 13:04:42 -05:00
" enum " : [ " mini " , " max " ] ,
2025-06-02 13:23:02 -05:00
" default " : " o1-mini "
} ,
" max_tokens " : {
" type " : " integer " ,
" description " : " The maximum number of tokens to use for generating the detailed instructions. Default is 16384. " ,
}
2024-10-28 17:56:06 -05:00
} ,
2025-06-02 13:23:02 -05:00
" required " : [ " prompt " ]
}
2025-06-03 13:04:42 -05:00
} ,
" _tags " : [ " llm " , " external " ]
2024-10-28 17:56:06 -05:00
}
]
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