QuickStart
ActionWeaver is an AI application framework that puts function-calling as a first-class feature.
This demonstrates how to seamlessly integrate any Python function into your LLM application using ActionWeaver and either the Azure or OpenAI API.
[1]:
%load_ext autoreload
%autoreload 2
Use ActionWeaver and OpenAI API
[2]:
from actionweaver.utils.tokens import TokenUsageTracker
from actionweaver.llms import wrap
from openai import OpenAI
openai_client = wrap(OpenAI())
[ ]:
from actionweaver import action
@action(name="GetCurrentTime")
def get_current_time() -> str:
"""
Use this for getting the current time in the specified time zone.
:return: A string representing the current time in the specified time zone.
"""
print ("Getting current time...")
import datetime
current_time = datetime.datetime.now()
return f"The current time is {current_time}"
@action(name="GetWeather", stop=False)
def get_current_weather(location, unit="fahrenheit"):
"""Get the current weather in a given location"""
print ("Getting current weather")
import json
if "tokyo" in location.lower():
return json.dumps({"location": "Tokyo", "temperature": "10", "unit": "celsius"})
elif "san francisco" in location.lower():
return json.dumps({"location": "San Francisco", "temperature": "72", "unit": "fahrenheit"})
elif "paris" in location.lower():
return json.dumps({"location": "Paris", "temperature": "22", "unit": "celsius"})
else:
return json.dumps({"location": location, "temperature": "unknown"})
[ ]:
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "what's the weather in San Francisco and Beijing ?"}
]
response = openai_client.create(
model="gpt-3.5-turbo",
messages=messages,
actions = [get_current_weather],
stream=False,
token_usage_tracker = TokenUsageTracker(500),
)
response
Use ActionWeaver and Azure OpenAI Service
[ ]:
import os
from openai import AzureOpenAI
azure_client = wrap(AzureOpenAI(
azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT"),
api_key=os.getenv("AZURE_OPENAI_KEY"),
api_version="2023-10-01-preview"
))
[ ]:
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "what's the weather in San Francisco and Beijing ?"}
]
response = azure_client.create(
model="gpt-35-turbo-0613-16k",
messages=messages,
stream=False,
actions = [get_current_weather],
token_usage_tracker = TokenUsageTracker(500),
)
response
Easily integrate tools from libraries such asLangchain
[ ]:
from langchain_community.tools.google_search.tool import GoogleSearchRun
from langchain_community.utilities.google_search import GoogleSearchAPIWrapper
search_tool = GoogleSearchRun(api_wrapper=GoogleSearchAPIWrapper())
[ ]:
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "search what is ActionWeaver on Github"}
]
from actionweaver.actions.factories.langchain import action_from_tool
response = azure_client.create(
model="gpt-35-turbo-0613-16k",
messages=messages,
stream=False,
actions = [action_from_tool(search_tool, description="Invoke this tool to search any information")],
token_usage_tracker = TokenUsageTracker(500),
)
response
[ ]:
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "search what is ActionWeaver on Github"}
]
from actionweaver.actions.factories.langchain import action_from_tool
response = azure_client.create(
model="gpt-35-turbo-0613-16k",
messages=messages,
stream=False,
actions = [action_from_tool(search_tool, description="Invoke this tool to search any information")],
token_usage_tracker = TokenUsageTracker(500),
)
response
Force execute an action
[ ]:
get_current_time.invoke(openai_client, messages=[{"role": "user", "content": "what time"}], model="gpt-3.5-turbo", stream=False, force=False)
[ ]:
get_current_time.invoke(azure_client, messages=[{"role": "user", "content": "what time"}], model="gpt-35-turbo-0613-16k", stream=False, force=False)
Stop the Action in the loop
Every action comes with a stop argument, which is set to False by default, if True this means that the LLM will immediately return the function’s output if chosen, but this also restricts the LLM from making multiple function calls. For instance, if asked about the weather in NYC and San Francisco, the model would invoke two separate functions sequentially for each city. However, with stop=True, this process is interrupted once the first function returns weather information for either NYC or
San Francisco, depending on which city it queries first.
[ ]:
get_current_weather.stop = True
[ ]:
get_current_weather.invoke(openai_client, messages=[{"role": "user", "content": "what weather is San Francisco"}], model="gpt-3.5-turbo", stream=False, force=True)