-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add: support for openai tool calls (functions only)
add: two examples related to function calls fix: Not saving a proper function call system message chore: remove the internal response with assist/response messages. best to leave this in the agent code chore: Update version to 0.3.0
- Loading branch information
Lee Huffman
committed
Dec 26, 2023
1 parent
4f744d9
commit 6de742e
Showing
11 changed files
with
321 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import json | ||
import os | ||
from typing import Literal, Dict | ||
|
||
from nimbusagent.agent.completion import CompletionAgent | ||
|
||
|
||
# Example dummy function hard coded to return the same weather | ||
# In production, this could be your backend API or an external API | ||
def get_current_weather(location: str, unit: Literal["celsius", "fahrenheit"] = "fahrenheit") -> Dict: | ||
""" | ||
Get the current weather in a given location | ||
:param location: The city and state, e.g. San Francisco, CA | ||
:param unit: The unit to return the temperature in, either celsius or fahrenheit | ||
:return: The current weather in the given location | ||
""" | ||
if "tokyo" in location.lower(): | ||
content = json.dumps({"location": "Tokyo", "temperature": "10", "unit": unit}) | ||
elif "san francisco" in location.lower(): | ||
content = json.dumps({"location": "San Francisco", "temperature": "30", "unit": unit}) | ||
elif "paris" in location.lower(): | ||
content = json.dumps({"location": "Paris", "temperature": "22", "unit": unit}) | ||
else: | ||
content = json.dumps({"location": location, "temperature": "unknown"}) | ||
|
||
return {"content": content} | ||
|
||
|
||
agent = CompletionAgent( | ||
openai_api_key=os.getenv('OPENAI_API_KEY'), | ||
model_name="gpt-4-1106-preview", | ||
system_message="You are a helpful assistant.", | ||
functions=[get_current_weather], | ||
use_tool_calls=True # If False, will disable tool calls and force the deprecated function calls | ||
) | ||
|
||
response = agent.ask("What's the weather like in San Francisco, Tokyo, and Paris?") | ||
print(response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import json | ||
import os | ||
import sys | ||
from typing import Literal, Dict | ||
|
||
from nimbusagent.agent.streaming import StreamingAgent | ||
|
||
|
||
# Example dummy function hard coded to return the same weather | ||
# In production, this could be your backend API or an external API | ||
def get_current_weather(location: str, unit: Literal["celsius", "fahrenheit"] = "fahrenheit") -> Dict: | ||
""" | ||
Get the current weather in a given location | ||
:param location: The city and state, e.g. San Francisco, CA | ||
:param unit: The unit to return the temperature in, either celsius or fahrenheit | ||
:return: The current weather in the given location | ||
""" | ||
if "tokyo" in location.lower(): | ||
content = json.dumps({"location": "Tokyo", "temperature": "10", "unit": unit}) | ||
elif "san francisco" in location.lower(): | ||
content = json.dumps({"location": "San Francisco", "temperature": "30", "unit": unit}) | ||
elif "paris" in location.lower(): | ||
content = json.dumps({"location": "Paris", "temperature": "22", "unit": unit}) | ||
else: | ||
content = json.dumps({"location": location, "temperature": "unknown"}) | ||
|
||
return {"content": content} | ||
|
||
|
||
agent = StreamingAgent( | ||
openai_api_key=os.getenv('OPENAI_API_KEY'), | ||
model_name="gpt-4-1106-preview", | ||
system_message="You are a helpful assistant.", | ||
functions=[get_current_weather], | ||
use_tool_calls=True # If False, will disable tool calls and force the deprecated function calls | ||
) | ||
|
||
response = agent.ask("What's the weather like in San Francisco, Tokyo, and Paris?") | ||
for chunk in response: | ||
sys.stdout.write(chunk) | ||
|
||
sys.stdout.write("\n\n") | ||
sys.stdout.flush() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.