Skip to content

Instantly share code, notes, and snippets.

@ozgurozkan123
Last active March 25, 2024 19:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozgurozkan123/ac191813a9f9de5a8eb9e5c3031f1ba3 to your computer and use it in GitHub Desktop.
Save ozgurozkan123/ac191813a9f9de5a8eb9e5c3031f1ba3 to your computer and use it in GitHub Desktop.
How to use Keymate.AI Python API as part of OpenAI Function calling
#pip install Keymate-API documentation https://github.com/ReminisApp/keymate-python-api
#pip install openai https://platform.openai.com/docs/api-reference
#pip install keymateapi
import keymateapi
import json
#Get your API token at https://my.keymate.ai/pricing
s = keymateapi.Keymateapi(
bearer_auth="<YOUR KEYMATE API KEY HERE>",
)
#Use Keymate API to browse web via query like What's the time in san francisco right now?
def searchN(q, percentile="3", numofpages="3"):
res = s.search(q=q ,percentile=percentile, numofpages=numofpages)
if res.two_hundred_application_json_object is not None:
#Use Keymate API response here we basically pass all of the content as unstructured text to LLM
return ""+str(res.two_hundred_application_json_object)
def browsingpoweredopenAIGPT(query="What's the price of bitcoin?"):
import openai
openai.api_version = "2020-11-07"
openai.api_type = "open_ai"
openai.api_base = "https://api.openai.com/v1"
openai.organization = "<YOUR OPENAI ORG ID HERE>" #Your OpenaAI org id
openai.api_key = "<YOUR OPENAI API KEY HERE>" #Your OpenAI Api Key
messages = [{"role": "user", "content": query}]
functions = [
{
"name": "python",
"description": "This is designed to enhance your knowledge base by searching the internet for up-to-date information across various topics.It provides you access to multiple search sources that you can merge. It enables you to access data beyond your training cutoff date by reconstructing user prompts and performing searches with multiple API calls where you will generate a list of search queries and run multiple calls.This one supports searches in any language and ensures user anonymity during the process.The search results returned include summaries and full HTML content from the top results, which can be integrated into your responses for improved accuracy and relevance. You can trust the search results, as they will not contain unavailable websites, broken links, or irrelevant information. To cite your sources, use the link provided in the response.Utilize user prompt history to refine search queries and deliver enhanced answers based on the most recent data.",
"parameters": {
"type": "object",
"properties": {
"q": {
"type": "string",
"description": "The search query",
},
"percentile": {
"type": "string",
"description": "Content length controlling parameter when it's higher, it brings less content. Default is '6'",
},
"numofpages": {
"type": "string",
"description": "Internet search pages controlling parameter. Default is '2'. Should be between 1 and 10",
},
},
"required": ["q"],
},
}
]
response = openai.ChatCompletion.create(
model="gpt-4", #Your choice of model that supports functions
messages=messages,
functions=functions,
function_call="auto", # auto is default, but we'll be explicit
)
response_message = response["choices"][0]["message"]
# Step 2: check if GPT wanted to call a function
if response_message.get("function_call"):
# Step 3: call the function
# Note: the JSON response may not always be valid; be sure to handle errors
available_functions = {
"python": searchN, #use Keymate operation as part of function
} # now we have multiple functions
function_name = response_message["function_call"]["name"]
function_to_call = available_functions[function_name]
function_args = json.loads(response_message["function_call"]["arguments"])
function_response = function_to_call(**function_args)
function_response_str = str(function_response)
words = function_response_str.split()
#Control number of words you want LLM to process
if len(words) > 6000:
function_response_str = ' '.join(words[:6000])
# Step 4: send the info on the function call and function response to GPT
messages.append(response_message) # extend conversation with assistant'sreply
messages.append(
{
"role": "function",
"name": function_name,
"content": function_response_str,
}
) # extend conversation with function response
second_response = openai.ChatCompletion.create(
model="gpt-4",
messages=messages,
) # get a new response from GPT where it can see the function response
return json.dumps({
"model": "keymateapi-gpt-function-call",
'choices': [
{"text": second_response["choices"][0]["message"]["content"],
"finish_reason": "stop"
}
]})
else:
# If GPT didn't want to call a function, just return its response
return json.dumps({
"model": "keymateapi-gpt-function-call",
'choices': [
{"text": response["choices"][0]["message"]["content"],
"finish_reason": "stop"
}
]})
jsonResult=browsingpoweredopenAIGPT("What's the price of bitcoin?")
print(jsonResult)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment