
? 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}
}