
? AI应用程序框架使使用LLM调用功能更容易?
探索我们的食谱以获取教程和示例!
不和谐
笔记本开始:
在github上明星我们!
ActionWeaver致力于为AI工程师提供最可靠,用户友好,高速和具有成本效益的功能呼叫框架。
特征:
您可以使用PIP安装Action Weaver:
pip install actionweaver 使用支持并行功能调用的最新OpenAI API!
from actionweaver . llms import wrap
from openai import OpenAI
openai_client = wrap ( OpenAI ())或使用Azure OpenAi服务
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"
))ActionWeaver包装客户端将管理函数调用循环,其中包括传递功能说明,使用LLM返回的参数执行函数以及处理异常。
该客户端将公开基于原始chat.completions.create api构建的create API。增强的create API将保留所有原始参数,并包含其他参数,例如:
action :向LLM提供可用的操作。orch :在整个功能呼叫循环中编排动作。exception_handler :引导函数调用循环的对象有关如何处理异常。这些论点将在随后的各节中得到证明。这些附加参数是可选的,并且总是有后备选项可以通过openai_client.client访问原始OpenAI客户端。
注意:
action代表LLM可以使用的工具。每个动作都包含两个主要要素:自动生成以促进提示的pydantic模型和常规的python函数。
开发人员可以将任何Python功能连接为具有简单装饰器的动作。在下面的示例中,我们介绍操作GetCurrentTime ,然后继续使用OpenAI API调用它。
ActionWeaver利用装饰方法的签名和Docstring作为描述,将其传递到OpenAI的功能API。动作装饰器也具有很高的适应能力,并且可以与其他装饰器结合使用,前提是保留了原始签名。
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 ]
)看看传递给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'}}
"""您还可以通过调用动作的invoke方法来强迫语言模型执行操作。它的论点包括Action Weaver包裹的客户端和其他传递给创建API的参数。
get_current_time . invoke ( openai_client , messages = [{ "role" : "user" , "content" : "what time is it" }], model = "gpt-3.5-turbo" , stream = False , force = True )您可以创建一个pydantic模型来定义要提取的结构数据,使用action_from_model创建一个操作,然后强迫语言模型从提示符中的信息中提取结构化数据。
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 )注意:false默认值的操作的
stop属性确定函数调用循环是否会立即返回操作结果,而不是将其传递给LLM,如果设置为true。
注意:您可以同时传递从功能和Pydantic模型产生的动作。
ActionWeaver通过在orch争论中传递了层次结构和行动链的设计。 orch是从动作作为钥匙到值的映射到包括的值
例如,假设我们有动作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
}
)用户可以提供异常的特定实现,其中遇到异常时调用handle_exception方法。 info参数封装了上下文详细信息,例如词典中的消息和API响应。
handle_exception方法决定了函数调用循环的操作进程,返回:
Return :向用户提供即时内容Continue :指示循环进行。 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请查看此示例以获取详细信息。
非常欢迎以错误修复,新功能,文档改进和拉动请求的形式进行贡献。
如果您发现ActionWeaver有用,请考虑引用该项目:
@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}
}