
? 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 래핑 클라이언트는 기능 설명 전달, LLM에서 반환 된 인수로 함수를 실행하며 예외를 처리하는 기능을 포함하는 기능 호출 루프를 관리합니다.
이 클라이언트는 원본 chat.completions.create API에 구축 된 create API를 노출시킵니다. 향상된 create API는 모든 원래 인수를 유지하고 다음과 같은 추가 매개 변수를 포함합니다.
action : LLM에 사용 가능한 조치 제공.orch : 기능 호출 루프 전체의 오케스트레이션 동작.exception_handler : 예외를 처리하는 방법에 대한 기능 호출 루프를 안내하는 객체. 이 주장은 다음 섹션에서 시연 될 것입니다. 이러한 추가 인수는 선택 사항이며 openai_client.client 통해 원래 OpenAI 클라이언트에 액세스하는 폴백 옵션이 항상 있습니다.
참고 :
actionLLM에서 사용할 수있는 도구를 나타냅니다. 각 동작은 두 가지 주요 요소로 구성됩니다. 프롬프트를 용이하게하기 위해 자동 생성 된 Pydantic 모델 및 기존의 Python 기능.
개발자는 간단한 데코레이터를 사용하여 모든 파이썬 기능을 동작으로 첨부 할 수 있습니다. 다음 예에서는 Action GetCurrentTime 도입 한 다음 OpenAI API를 사용하여이를 호출합니다.
ActionWeaver는 Decorated Method의 시그니처 및 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-rapped 클라이언트와 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 ) 추출하려는 구조 데이터를 정의하고 action_from_model 사용하여 작업을 작성한 다음 언어 모델을 강제하여 프롬프트의 정보에서 구조화 된 데이터를 추출하도록 Pydantic 모델을 생성 할 수 있습니다.
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속성은 함수 호출 루프가 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
}
) 사용자는 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}
}