Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
🪡 AI application framework that makes function calling with LLM easier🪡
Explore Our Cookbooks For Tutorials & Examples!
NOTEBOOKS TO GET STARTED:
ActionWeaver strives to be the most reliable, user-friendly, high-speed, and cost-effective function-calling framework for AI engineers.
Features:
You can install ActionWeaver using pip:
pip install actionweaver
Use the LATEST OpenAI API that supports parallel function calling !
from actionweaver.llms import wrap
from openai import OpenAI
openai_client = wrap(OpenAI())
or using 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"
))
The ActionWeaver wrapped client will manage the function calling loop, which includes passing function descriptions, executing functions with arguments returned by llm, and handling exceptions.
This client will expose a create
API built upon the original chat.completions.create
API. The enhanced create
API will retain all original arguments and include additional parameters such as:
action
: providing available actions to LLM.orch
: orchestrating actions throughout the function calling loop.exception_handler
: an object guiding the function calling loop on how to handle exceptions.
These arguments will be demonstrated in the subsequent sections.These additional arguments are optional, and there's always the fallback option to access the original OpenAI client via openai_client.client
.
Note: An
action
represents a tool that can be used by LLM. Each action comprises two main elements: a Pydantic model that is auto-generated to facilitate prompting, and a conventional Python function.
Developers can attach ANY Python function as an action with a simple decorator. In the following example, we introduce action GetCurrentTime
, and then proceed to use the OpenAI API to invoke it.
ActionWeaver utilizes the decorated method's signature and docstring as a description, passing them along to OpenAI's function API. The Action decorator is also highly adaptable and can be combined with other decorators, provided that the original signature is preserved.
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.
"""
import datetime
current_time = datetime.datetime.now()
return f"The current time is {current_time}"
# Ask LLM what time is it
response = openai_client.create(
model="gpt-4o",
messages=[{"role": "user", "content": "what time is it"}],
actions = [get_current_time]
)
Take a look what is passing to OpenAI API
get_current_weather.get_function_details()
"""
{'name': 'GetWeather',
'description': 'Get the current weather in a given location',
'parameters': {'properties': {'location': {'title': 'Location'},
'unit': {'default': 'fahrenheit', 'title': 'Unit'}},
'required': ['location'],
'title': 'Get_Current_Weather',
'type': 'object'}}
"""
You can also compel the language model to execute the action by calling the invoke
method of an action. Its arguments includes the ActionWeaver-wrapped client and other arguments passed to the create API.
get_current_time.invoke(openai_client, messages=[{"role": "user", "content": "what time is it"}], model="gpt-3.5-turbo", stream=False, force=True)
You can create a Pydantic model to define the structural data you want to extract, create an action using action_from_model
and then force the language model to extract structured data from information in the prompt.
from pydantic import BaseModel
from actionweaver.actions.factories.pydantic_model_to_action import action_from_model
class User(BaseModel):
name: str
age: int
action_from_model(User, stop=True).invoke(client, messages=[{"role": "user", "content": "Tom is 31 years old"}], model="gpt-3.5-turbo", stream=False, force=False)
Note: The
stop
property of an action, with a default value of False, determines whether the function calling loop will immediately return the action's result instead of passing it to LLM if set to True.
Note: You can simultaneously pass actions generated from both functions and Pydantic models.
ActionWeaver enables the design of hierarchies and chains of actions by passing in orch
argument. orch
is a mapping from actions as keys to values including
For example, let's say we have actions a1, a2, a3.
client.create(
[
{"role": "user", "content": "message"}
],
actions=[a1, a2, a3], # First, LLM respond with either a1, a2 or a3, or text without action
# Define the orchestration logic for actions:
orch={
a1.name: [a2, a3], # If a1 is invoked, the next response will be either a2, a3 or a text response.
a2.name: a3, # If a2 is invoked, the next action will be a3
a3.name: [a4] # If a3 is invoked, the next response will be a4 or a text response.
a4.name: None # If a4 is invoked, the next response will guarantee to be a text message
}
)
Users can provide a specific implementation of ExceptionHandler, where the handle_exception
method is invoked upon encountering an exception. The info
parameter encapsulates contextual details such as messages and API responses within a dictionary.
The handle_exception
method dictates the course of action for the function calling loop, returning either:
Return
: providing immediate content back to the userContinue
: instructing the loop to proceed.from actionweaver.llms import ExceptionAction, ChatLoopInfo, Continue, Return
class ExceptionHandler(ABC):
"""Base class for exception handlers.
This class provides a framework for handling exceptions within the function calling loop.
"""
@abstractmethod
def handle_exception(self, e: Exception, info: ChatLoopInfo) -> ExceptionAction:
pass
Take a look at this example for details.
Contributions in the form of bug fixes, new features, documentation improvements, and pull requests are VERY welcomed.
If you find ActionWeaver useful, please consider citing the project:
@software{Teng_Hu_ActionWeaver_2024,
author = {Teng Hu},
license = {Apache-2.0},
month = Aug,
title = {ActionWeaver: Application Framework for LLMs},
url = {https://github.com/TengHu/ActionWeaver},
year = {2024}
}
FAQs
An Application Framework for Building LLM Agents
We found that actionweaver demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.