当前位置: 首页 > news >正文

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

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):

相关文章:

  • 利用香港多IP服务器建站蜘蛛池执行SEO策略的实践
  • Python注释
  • Redis中String命令的基础操作
  • Chroma 向量数据入门
  • 强化学习在自动驾驶技术中的应用与挑战
  • 速通LLaMA3:《The Llama 3 Herd of Models》全文解读
  • 寿司检测系统源码分享
  • UNI-SOP使用说明
  • windows下tp5创建定时任务
  • 网络原理3-应用层(HTTP/HTTPS)
  • 安卓LiveData与MutableLiveData的使用
  • Excel DAYS函数怎么用,DAYS函数的使用方法来了
  • (含答案)C++笔试题你可以答对多少?
  • 探索 Python 中的 AI 魔法:markdownify 库的奥秘
  • 从安防视频监控行业发展趋势看EasyCVR平台如何驱动行业智能升级
  • canvas实际项目操作,包含:线条,圆形,扇形,图片绘制,图片圆角遮罩,矩形,弧形文字...
  • Netty 框架总结「ChannelHandler 及 EventLoop」
  • Python学习之路16-使用API
  • Redash本地开发环境搭建
  • TCP拥塞控制
  • Three.js 再探 - 写一个跳一跳极简版游戏
  • Web设计流程优化:网页效果图设计新思路
  • 浅谈Golang中select的用法
  • 如何合理的规划jvm性能调优
  • 用element的upload组件实现多图片上传和压缩
  • 【运维趟坑回忆录 开篇】初入初创, 一脸懵
  • MPAndroidChart 教程:Y轴 YAxis
  • 第二十章:异步和文件I/O.(二十三)
  • # C++之functional库用法整理
  • #define,static,const,三种常量的区别
  • #NOIP 2014# day.1 T2 联合权值
  • (1)无线电失控保护(二)
  • (2009.11版)《网络管理员考试 考前冲刺预测卷及考点解析》复习重点
  • (7)svelte 教程: Props(属性)
  • (Charles)如何抓取手机http的报文
  • (C语言版)链表(三)——实现双向链表创建、删除、插入、释放内存等简单操作...
  • (delphi11最新学习资料) Object Pascal 学习笔记---第13章第6节 (嵌套的Finally代码块)
  • (Mac上)使用Python进行matplotlib 画图时,中文显示不出来
  • (TipsTricks)用客户端模板精简JavaScript代码
  • (补充):java各种进制、原码、反码、补码和文本、图像、音频在计算机中的存储方式
  • (不用互三)AI绘画工具应该如何选择
  • (二)【Jmeter】专栏实战项目靶场drupal部署
  • (二)PySpark3:SparkSQL编程
  • (回溯) LeetCode 77. 组合
  • (机器学习-深度学习快速入门)第三章机器学习-第二节:机器学习模型之线性回归
  • (推荐)叮当——中文语音对话机器人
  • (已解决)Bootstrap精美弹出框模态框modal,实现js向modal传递数据
  • (转)树状数组
  • .NET 反射 Reflect
  • .NET 解决重复提交问题
  • .net6使用Sejil可视化日志
  • .net与java建立WebService再互相调用
  • ::before和::after 常见的用法
  • @NotNull、@NotEmpty 和 @NotBlank 区别
  • @软考考生,这份软考高分攻略你须知道