
- AI Application Framework ที่ทำให้ฟังก์ชั่นการโทรด้วย LLM ง่ายขึ้น?
สำรวจตำราอาหารของเราสำหรับการสอนและตัวอย่าง!
ความไม่ลงรอยกัน
สมุดบันทึกที่จะเริ่มต้น:
แสดงเราบน GitHub!
ActionWeaver มุ่งมั่นที่จะเป็นเฟรมเวิร์กที่น่าเชื่อถือที่สุดใช้งานง่ายความเร็วสูงและมีประสิทธิภาพในการเรียกใช้ฟังก์ชั่นสำหรับวิศวกร AI
คุณสมบัติ:
คุณสามารถติดตั้ง ActionWeaver โดยใช้ PIP:
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 และการจัดการข้อยกเว้น
ไคลเอนต์นี้จะเปิดเผย API create ที่สร้างขึ้นเมื่อ chat.completions.create API ดั้งเดิม create API ที่ได้รับการปรับปรุงจะเก็บอาร์กิวเมนต์ดั้งเดิมทั้งหมดและรวมพารามิเตอร์เพิ่มเติมเช่น:
action : ให้การดำเนินการที่มีอยู่กับ LLMorch : การดำเนินการตามการดำเนินการตลอดฟังก์ชั่นการเรียกลูปexception_handler : วัตถุที่ชี้นำฟังก์ชั่นการเรียกใช้ลูปเกี่ยวกับวิธีจัดการข้อยกเว้น ข้อโต้แย้งเหล่านี้จะแสดงในส่วนที่ตามมา อาร์กิวเมนต์เพิ่มเติมเหล่านี้เป็นทางเลือกและมีตัวเลือกทางเลือกในการเข้าถึงไคลเอนต์ OpenAI ดั้งเดิมผ่าน openai_client.client
หมายเหตุ :
actionแสดงถึงเครื่องมือที่สามารถใช้งานได้โดย LLM การกระทำแต่ละครั้งประกอบด้วยสององค์ประกอบหลัก: โมเดล pydantic ที่สร้างขึ้นอัตโนมัติเพื่ออำนวยความสะดวกในการแจ้งเตือนและฟังก์ชั่น Python ทั่วไป
นักพัฒนาสามารถแนบฟังก์ชั่น Python ใด ๆ เป็นแอ็คชั่นกับมัณฑนากรที่เรียบง่าย ในตัวอย่างต่อไปนี้เราแนะนำการดำเนินการ GetCurrentTime จากนั้นดำเนินการต่อเพื่อใช้ OpenAI API เพื่อเรียกใช้
ActionWeaver ใช้ลายเซ็นของวิธีการตกแต่งและเอกสารเป็นคำอธิบายโดยส่งผ่านไปยังฟังก์ชั่น API ของ 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.
"""
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 )หมายเหตุ : คุณสมบัติ
stopของการกระทำที่มีค่าเริ่มต้นเป็นเท็จกำหนดว่าฟังก์ชันการเรียกลูปจะส่งคืนผลลัพธ์ของการกระทำทันทีแทนที่จะส่งผ่านไปยัง 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
}
) ผู้ใช้สามารถให้การใช้งานเฉพาะของ ExceptionHandler ซึ่งวิธี 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}
}