python AutoGen接入开源模型xLAM-7b-fc-r,测试function calling的功能

server/2024/9/20 11:09:00/

AutoGen主打的是多智能体,对话和写代码,但是教程方面没有langchain丰富,我这里抛砖引玉提供一个autogen接入开源function calling模型的教程,我这里使用的开源repo是:https://github.com/SalesforceAIResearch/xLAM
开源模型是:https://huggingface.co/Salesforce/xLAM-7b-fc-r

1b的模型效果有点差,推荐使用7b的模型。首先使用vllm运行:

vllm serve Salesforce/xLAM-8x7b-r --host 0.0.0.0 --port 8000 --tensor-parallel-size 4

然后autogen代码示例:

import re
import json
import random
import time from typing import Literal
from pydantic import BaseModel, Field
from typing_extensions import Annotated
import autogen
from autogen.cache import Cache
from openai.types.completion import Completion
import openai
from xLAM.client import xLAMChatCompletion, xLAMConfig
from openai.types.chat import ChatCompletion, ChatCompletionMessageToolCall
from openai.types.chat.chat_completion import ChatCompletionMessage, Choice
from openai.types.completion_usage import CompletionUsagelocal_llm_config={"config_list": [{"model": "/<your_path>/xLAM-7b-fc-r", # Same as in vLLM command"api_key": "NotRequired", # Not needed"model_client_cls": "CustomModelClient","base_url": "http://localhost:8000/v1",  # Your vLLM URL, with '/v1' added"price": [0, 0],}],"cache_seed": None # Turns off caching, useful for testing different models
}TOOL_ENABLED = Trueclass CustomModelClient:def __init__(self, config, **kwargs):print(f"CustomModelClient config: {config}")gen_config_params = config.get("params", {})self.max_length = gen_config_params.get("max_length", 256)print(f"Loaded model {config['model']}")config = xLAMConfig(base_url=config["base_url"], model=config['model'])self.llm = xLAMChatCompletion.from_config(config)def create(self, params):if params.get("stream", False) and "messages" in params:raise NotImplementedError("Local models do not support streaming.")else:if "tools" in params:tools=[item['function'] for item in params["tools"]]response = self.llm.completion(params["messages"], tools=tools)if len(response['choices'][0]['message']['tool_calls'])>0:finish_reason='tool_calls'tool_results = response['choices'][0]['message']['tool_calls']if isinstance(tool_results, list) and isinstance(tool_results[0], list):tool_results = tool_results[0]tool_calls = []try:for tool_call in tool_results:tool_calls.append(ChatCompletionMessageToolCall(id=str(random.randint(0,2500)),function={"name": tool_call['name'], "arguments": json.dumps(tool_call["arguments"])},type="function"))except Exception as e:print("Tool parse error: {tool_results}")tool_calls=Nonefinish_reason='stop'else:finish_reason='stop'tool_calls = Nonemessage  = ChatCompletionMessage(role="assistant",content=response['choices'][0]['message']['content'],function_call=None,tool_calls=tool_calls,)choices = [Choice(finish_reason=finish_reason, index=0, message=message)]response_oai = ChatCompletion(id=str(random.randint(0,25000)),model=params["model"],created=int(time.time()),object="chat.completion",choices=choices,usage=CompletionUsage(prompt_tokens=0,completion_tokens=0,total_tokens=0),cost=0.0,)return response_oaidef message_retrieval(self, response):"""Retrieve the messages from the response."""choices = response.choicesif isinstance(response, Completion):return [choice.text for choice in choices] if TOOL_ENABLED:return [  # type: ignore [return-value](choice.message  # type: ignore [union-attr]if choice.message.function_call is not None or choice.message.tool_calls is not None  # type: ignore [union-attr]else choice.message.content)  # type: ignore [union-attr]for choice in choices]else:return [  # type: ignore [return-value]choice.message if choice.message.function_call is not None else choice.message.content  # type: ignore [union-attr]for choice in choices]def cost(self, response) -> float:"""Calculate the cost of the response."""response.cost = 0return 0@staticmethoddef get_usage(response):# returns a dict of prompt_tokens, completion_tokens, total_tokens, cost, model# if usage needs to be tracked, else Nonereturn {"prompt_tokens": response.usage.prompt_tokens if response.usage is not None else 0,"completion_tokens": response.usage.completion_tokens if response.usage is not None else 0,"total_tokens": (response.usage.prompt_tokens + response.usage.completion_tokens if response.usage is not None else 0),"cost": response.cost if hasattr(response, "cost") else 0,"model": response.model,}chatbot = autogen.AssistantAgent(name="chatbot",system_message="For currency exchange tasks, only use the functions you have been provided with. Reply TERMINATE when the task is done.",llm_config=local_llm_config,
)# create a UserProxyAgent instance named "user_proxy"
user_proxy = autogen.UserProxyAgent(name="user_proxy",is_termination_msg=lambda x: x.get("content", "") and x.get("content", "").rstrip().endswith("TERMINATE"),human_input_mode="NEVER",max_consecutive_auto_reply=5,
)CurrencySymbol = Literal["USD", "EUR"]def exchange_rate(base_currency: CurrencySymbol, quote_currency: CurrencySymbol) -> float:if base_currency == quote_currency:return 1.0elif base_currency == "USD" and quote_currency == "EUR":return 1 / 1.1elif base_currency == "EUR" and quote_currency == "USD":return 1.1else:raise ValueError(f"Unknown currencies {base_currency}, {quote_currency}")@user_proxy.register_for_execution()
@chatbot.register_for_llm(description="Currency exchange calculator.")
def currency_calculator(base_amount: Annotated[float, "Amount of currency in base_currency"],base_currency: Annotated[CurrencySymbol, "Base currency"] = "USD",quote_currency: Annotated[CurrencySymbol, "Quote currency"] = "EUR",
) -> str:quote_amount = exchange_rate(base_currency, quote_currency) * base_amountreturn f"{quote_amount} {quote_currency}"print(chatbot.llm_config["tools"])
chatbot.register_model_client(model_client_cls=CustomModelClient)
query = "How much is 123.45 USD in EUR?"
# query = "What's the weather like in New York in fahrenheit?"
res = user_proxy.initiate_chat(chatbot, message=query,max_round=5,)
print("Chat history:", res.chat_history)

运行示例结果:

user_proxy (to chatbot):How much is 123.45 USD in EUR?--------------------------------------------------------------------------------
chatbot (to user_proxy):***** Suggested tool call (507): currency_calculator *****
Arguments:
{"base_amount": 123.45, "base_currency": "USD", "quote_currency": "EUR"}
**********************************************************-------------------------------------------------------------------------------->>>>>>>> EXECUTING FUNCTION currency_calculator...
user_proxy (to chatbot):user_proxy (to chatbot):***** Response from calling tool (507) *****
112.22727272727272 EUR
********************************************--------------------------------------------------------------------------------
chatbot (to user_proxy):The currency calculator returned 112.23 EUR.--------------------------------------------------------------------------------
user_proxy (to chatbot):

http://www.ppmy.cn/server/119379.html

相关文章

如何在 Qt 的 QListWidget 中逐行添加和显示数据

文章目录 如何在 Qt 的 QListWidget 中逐行添加和显示数据目标实现步骤1. 在 Qt Designer 中添加 QListWidget2. 在代码中逐行添加数据示例代码 代码解析3. 使用自定义项 运行效果总结 如何在 Qt 的 QListWidget 中逐行添加和显示数据 QListWidget 是 Qt 提供的一个非常方便的…

mysql DBA常用的sql

是否一般查询日志&#xff0c;默认关闭 show variables like ‘general_log’; 是否开启慢日志查询 默认关闭 show global variables like ‘slow_query_log’; 开启慢日志查询 SET GLOBAL slow_query_log ‘ON’; 默认是10 单位s SELECT long_query_time; 设置超过1s就算…

软考高级:数据库规范化: 1NF、2NF、3NF和 BCNF AI 解读

数据库的规范化是数据库设计中的一个重要过程&#xff0c;旨在减少数据冗余和提高数据一致性。它通过一系列规则&#xff08;称为范式&#xff09;来优化数据库表的结构。 常见的范式有1NF、2NF、3NF和BCNF。让我们分别来解释这些范式。 生活化例子 想象你在整理一个家庭成…

一、编译原理(引论)

目录 【一】、引论 一、编译器 1、编译器 2、编译器与解释器 3、编译器结构 【一】、引论 一、编译器 1、编译器 &#xff08;1&#xff09;编译器&#xff1a;将人类易懂的 高级语言 翻译成 硬件可执行的目标机器语言 &#xff08;2&#xff09; 高级语言 ⚫ 直接面…

回归预测|基于饥饿游戏搜索优化随机森林的数据回归预测Matlab程序HGS-RF 多特征输入单输出 高引用先用先创新

回归预测|基于饥饿游戏搜索优化随机森林的数据回归预测Matlab程序HGS-RF 多特征输入单输出 高引用先用先创新 文章目录 一、基本原理1. 饥饿游戏搜索优化算法&#xff08;HGS&#xff09;简介2. 随机森林&#xff08;RF&#xff09;简介3. HGS-RF回归预测流程1. 初始化2. 随机森…

linux第三课(linux中安装nginx与redis及SpringBoot集成redis)

目录 一.nginx引入 二.关于nginx 1.什么是nginx 2.nginx的特点 3.在nginx中安装nginx 三.关于redis 1.背景引入 2.什么是redis 3.redis的特点 4.在linux下的docker中安装redis 四.redis中的数据结构 (1)String(字符串) (2)Hash (3)list(列表) (5)zset(sorted se…

Qt_布局管理器

目录 1、QVBoxLayout垂直布局 1.1 QVBoxLayout的使用 1.2 多个布局管理器 2、QHBoxLayout水平布局 2.1 QHBoxLayout的使用 2.2 嵌套的Layout 3、QGridLayout网格布局 3.1 QGridLayout的使用 3.2 设置控件大小比例 4、QFormLayout 4.1 QFormLayout的使用 5、…

Modbus_tcp

目录 一&#xff1a;modbus起源 1.起源 2. 分类&#xff1a; 3. 优势&#xff1a; 4. 应用场景&#xff1a; 5.ModbusTCP特点&#xff08;掌握&#xff09;&#xff1a; 二、 ModbusTCP的协议 1. 报文头 2. 寄存器 1. 线圈&#xff08;Coils&#xff09; 2. 离…