
? LLMを使用して関数呼び出しを簡単にするAIアプリケーションフレームワーク?
チュートリアルと例については、料理本をご覧ください!
不和
開始するノートブック:
Githubで私たちを主演!
ActionWeaverは、AIエンジニア向けの最も信頼性が高く、使いやすく、高速で、費用対効果の高い機能をコールするフレームワークになるよう努めています。
特徴:
PIPを使用してActionWeaverをインストールできます。
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 Lappedクライアントは、関数呼び出しループを管理します。これには、関数の説明の渡し、LLMが返した引数を使用して関数の実行、および例外の処理が含まれます。
このクライアントは、元のchat.completions.create APIに基づいて構築されたcreate APIを公開します。拡張されたcreate APIには、すべての元の引数を保持し、次のような追加のパラメーターが含まれます。
action :LLMに利用可能なアクションを提供します。orch :ループを呼び出す関数全体のアクションを調整します。exception_handler :例外を処理する方法についてループを呼び出す関数をガイドするオブジェクト。これらの引数は、後続のセクションで実証されます。これらの追加の引数はオプションであり、 openai_client.clientを介して元のOpenAIクライアントにアクセスするフォールバックオプションが常にあります。
注:
action、LLMで使用できるツールを表します。各アクションは、プロンプトを促進するために自動生成されたピダンティックモデルと、従来のPython関数の2つの主要な要素と、2つの主要な要素で構成されています。
開発者は、任意の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メソッドを呼び出すことにより、言語モデルにアクションを実行するように強制することもできます。その引数には、ActionWeaverが包まれたクライアントと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 )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プロパティは、関数呼び出しループが、Trueに設定されている場合にLLMに渡す代わりに、アクションの結果をすぐに返すかどうかを決定します。
注:両方の関数と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}
}