使用本地部署的Hermes 2 Pro构建开放的LLM应用程序

news2024/10/7 11:29:00

之前我介绍了如何使用OpenAI最新的LLM GPT-4o,通过函数调用将实时数据引入到LLM。在这篇后续文章中我将介绍使用Hermes 2 Pro -Llama- 3 8B进行函数调用,这是一种由Nous Research开发的功能强大的LLM,基于Meta的Llama 3架构,有80亿个参数。它是开放模型,我们将在Hugging Face的文本生成推理上运行它。

我们将把Fightaware.com的API 与该LLM集成起来,以便实时跟踪航班状态。

FlightAware的AeroAPI是开发人员获取全面航班信息的一种完美工具。它支持实时航班跟踪、历史和未来航班数据以及按各种标准进行航班搜索。该API以用户友好的JSON格式呈现数据,因而高度可用、易于集成。我们将调用REST API,根据用户发送给LLM的提示获取航班的实时状态。

Hermes 2 Pro简介

Hermes 2 Pro -Llama- 3 8B擅长自然语言处理任务、创意写作和编程协助等。它的一项突出功能是出色的函数调用功能,便于执行外部函数,并检索与股票价格、公司基本面、财务报表等相关的信息。

该模型利用特殊的系统提示和多轮函数调用结构以及新的ChatML角色,使得函数调用可靠且易于解析。据基准测试显示,Hermes 2 Pro-Llama-3在与Fireworks AI合作构建的函数调用评估中获得了出色的90%。

本地部署Hermes 2 Pro

就这个环境而言,我使用一台基于英伟达GeForce RTX 4090 GPU的Linux服务器,搭载24GB的VRAM。它运行Docker和英伟达容器工具包,使容器能够访问GPU。

我们将使用来自Hugging Face的文本生成推理服务器来运行Hermes 2 Pro。

下面的命令在端口8080上启动推理引擎,通过REST端点为LLM提供服务。

export token="YOUR_HF_TOKEN"
 
export model="NousResearch/Hermes-2-Pro-Llama-3-8B"
 
export volume="/home/ubuntu/data"
 
docker run --name hermes -d --gpus all -e HUGGING_FACE_HUB_TOKEN=$token --shm-size 1g -p 8080:80 -v $volume:/data ghcr.io/huggingface/text-generation-inference:2.0.3 --model-id $model --max-total-tokens 8096

   	
To test the endpoint, run the following command:

	curl 127.0.0.1:8081  \
  -X POST \
   -H 'Content-Type: application/json' \
 -d '{"inputs":"What is Deep Learning?"}'

如果一切正常,您应该看到Hermes 2 Pro的响应。

跟踪航班状态的函数

在继续下一步之前,注册FlightAware并获取API密钥,使用REST API需要API密钥。免费的个人版本足以完成本教程。

获得API密钥后,用Python创建以下函数,以检索任何航班的状态。

	import ast
	import json
import random
	from datetime import datetime, timedelta
	import requests
	import pytz

	
	def get_flight_status(flight):
	    """Returns Flight Information"""
	

	    AEROAPI_BASE_URL = "https://aeroapi.flightaware.com/aeroapi"
	    AEROAPI_KEY="YOUR FLIGHTAWARE API KEY"
	    
	    def get_api_session():
	        session = requests.Session()
	        session.headers.update({"x-apikey": AEROAPI_KEY})
	        return session
	
	    
def fetch_flight_data(flight_id, session):
	        if "flight_id=" in flight_id:
	            flight_id = flight_id.split("flight_id=")[1]    
	        
	       
 start_date = datetime.now().date().strftime('%Y-%m-%d')
	        end_date = (datetime.now().date() + timedelta(days=1)).strftime('%Y-%m-%d')
	        api_resource = f"/flights/{flight_id}?start={start_date}&end={end_date}"
	        response = session.get(f"{AEROAPI_BASE_URL}{api_resource}")
	        response.raise_for_status()
	        return response.json()['flights'][0]
	
	    def utc_to_local(utc_date_str, local_timezone_str):
	        utc_datetime = datetime.strptime(utc_date_str, '%Y-%m-%dT%H:%M:%SZ').replace(tzinfo=pytz.utc)
	        local_timezone = pytz.timezone(local_timezone_str)
	        local_datetime = utc_datetime.astimezone(local_timezone)
	        return local_datetime.strftime('%Y-%m-%d %H:%M:%S')    
	    
	    session = get_api_session()
	    flight_data = fetch_flight_data(flight, session)
	    
	    dep_key = 'estimated_out' if 'estimated_out' in flight_data and flight_data['estimated_out'] else \
	          'actual_out' if 'actual_out' in flight_data and flight_data['actual_out'] else \
	          'scheduled_out'
	    
	    arr_key = 'estimated_in' if 'estimated_in' in flight_data and flight_data['estimated_in'] else \
	          'actual_in' if 'actual_in' in flight_data and flight_data['actual_in'] else \
	          'scheduled_in'    
	    
	    flight_details = {
	        'flight':flight,
	        'source': flight_data['origin']['city'],
	        'destination': flight_data['destination']['city'],
	        'depart_time': utc_to_local(flight_data[dep_key], flight_data['origin']['timezone']),
	        'arrival_time': utc_to_local(flight_data[arr_key], flight_data['destination']['timezone']),
	        'status': flight_data['status']
	    }
	return json.dumps(flight_details)

	
	flight_info = get_flight_status("EK524")
	print(flight_info)
	#'{"flight": "EK524", "source": "Dubai", "destination": "Hyderabad", "depart_time": "2024-05-23 22:00:00", "arrival_time": "2024-05-24 03:05:00", "status": "Scheduled"}'

虽然代码简单直观,还是让我解释关键步骤。

get_flight_status函数接受航班参数(假设是航班标识符),并以JSON格式返回格式化的航班详细信息。它查询AeroAPI以根据特定的航班标识符获取航班数据,并格式化关键细节,比如源地、目的地、驶离时间、到达时间和状态。

不妨看看脚本的组件:

API凭据:

AEROAPI_BASE_URL是FlightAware AeroAPI的基础URL。

AEROAPI_KEY是用于身份验证的API密钥。

会话管理:

get_api_session:这个嵌套函数初始化请求。这将使用API密钥设置所需的报头,并返回会话对象。该会话将处理所有API请求。

数据获取:

fetch_flight_data:这个函数接受flight_id和session作为参数。它用适当的日期过滤器构造端点URL,用于获取一天的数据,并发送GET请求以检索航班数据。该函数处理API响应,并提取相关的航班信息。

时间转换:

utc_to_local:根据提供的时区字符串将UTC时间(来自API响应)转换为本地时间。该函数帮助我们获得基于城市的到达和驶离时间。

数据处理:

脚本根据可用的估计时间或实际时间确定驶离和到达时间的键,并返回到计划时间。然后,它构造一个含有格式化航班详细信息的字典。

上述截图显示了我们从FlightAware API收到的从迪拜飞往海得拉巴的阿联酋航空EK524的响应。请注意,到达和驶离时间基于城市的当地时间。

我们旨在将该函数与Gemini 1.0 Pro集成,使其能够实时访问航班跟踪信息。

将函数与Hermes 2 Pro集成

先使用以下命令安装最新版本的Hugging Face Python SDK:

pip install --upgrade huggingface_hub

导入模块,并通过将客户端指向TGI端点来初始化客户端。

from huggingface_hub import InferenceClient

client = InferenceClient("http://127.0.0.1:8080")

接下来,定义函数模式,采用的格式与OpenAPI函数调用的格式一样。

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_flight_status",
            "description": "Get status of a flight",
            "parameters": {
                "type": "object",
                "properties": {
                    "flight": {
                        "type": "string",
                        "description": "Flight number"
                    }
                },
                "required": ["flight"]
            }
        }
    }
]

这将使用LLM用作工具的一个或多个函数填充列表。

现在,我们将创建接受提示并确定是否需要调用函数的聊天机器人。如果需要调用,则LLM先返回函数名和需要调用的参数。函数的输出作为第二次调用的一部分发送给LLM。最终的响应将根据函数的输出得到与事实相符的正确答案。

def chatbot(prompt):
    messages = [
        {
            "role": "system",
            "content": "You're a helpful assistant! Answer the users question best you can based on the tools provided. Be concise in your responses.",
        },
        {
            "role": "user",
            "content": prompt
        },
    ]
 
    response = client.chat_completion(messages=messages, tools=tools)
    tool_calls = response.choices[0].message.tool_calls
 
    if tool_calls:
        available_functions = {
            "get_flight_status": get_flight_status,
        }
        
        for tool_call in tool_calls:
            function_name = tool_call.function.name
            function_to_call = available_functions[function_name]
            function_args = tool_call.function.arguments
        
            function_response = function_to_call(flight=function_args.get("flight"))
            
            messages.append(
                {
                    "role": "tool",
                    "name": function_name,
                    "content": function_response
                }
            )
            
        final_response = client.chat_completion(messages=messages)
        return final_response
        
    return response

目标LLM期望的提示的自动格式化是使用Hugging Face Python库的一个好处。比如说,使用函数时,Hermes 2 Pro的提示需要按照特定的格式进行结构化:

<|im_start|>system
You are a function calling AI model. You are provided with function signatures within XML tags. You may call one or more functions to assist with the user query. Don't make assumptions about what values to plug into functions. Here are the available tools: [{'type': 'function', 'function': {'name': 'get_stock_fundamentals', 'description': 'Get fundamental data for a given stock symbol using yfinance API.', 'parameters': {'type': 'object', 'properties': {'symbol': {'type': 'string'}}, 'required': ['symbol']}}}] Use the following pydantic model json schema for each tool call you will make: {'title': 'FunctionCall', 'type': 'object', 'properties': {'arguments': {'title': 'Arguments', 'type': 'object'}, 'name': {'title': 'Name', 'type': 'string'}}, 'required': 

['arguments', 'name']} For each function call return a json object with function name and arguments within XML tags as follows:

{'arguments': , 'name': }
<|im_end|>

同样,函数的输出可以以以下格式发送到LLM:

<|im_start|>tool

{"name": "get_stock_fundamentals", "content": {'symbol': 'TSLA', 'company_name': 'Tesla, Inc.', 'sector': 'Consumer Cyclical', 'industry': 'Auto Manufacturers', 'market_cap': 611384164352, 'pe_ratio': 49.604652, 'pb_ratio': 9.762013, 'dividend_yield': None, 'eps': 4.3, 'beta': 2.427, '52_week_high': 299.29, '52_week_low': 152.37}}

<|im_end|>

确保提示遵循该模板需要仔细格式化。InferenceClient类可高效地处理这种转换,使开发人员能够在提示中使用系统、用户、工具和助手角色的熟悉的OpenAI格式。

在首次调用聊天完成API时,LLM给出以下答案作为响应:

随后,在调用函数之后,我们将结果嵌入到消息中并将其发回给LLM。

正如您所见,集成函数调用的工作流程与OpenAI非常相似。

现在是时候调用聊天机器人并通过提示来测试它了。

res=chatbot("What's the status of EK226?")
print(res.choices[0].message.content)

聊天机器人的完整代码如下所示。

from huggingface_hub import InferenceClient

client = InferenceClient("http://127.0.0.1:8080")


tools = [
    {
        "type": "function",
        "function": {
            "name": "get_flight_status",
            "description": "Get status of a flight",
            "parameters": {
                "type": "object",
                "properties": {
                    "flight": {
                        "type": "string",
                        "description": "Flight number"
                    }
                },
                "required": ["flight"]
            }
        }
    }
]


def chatbot(prompt):
    messages = [
        {
            "role": "system",
            "content": "You're a helpful assistant! Answer the users question best you can based on the tools provided. Be concise in your responses.",
        },
        {
            "role": "user",
            "content": prompt
        },
]


    response = client.chat_completion(messages=messages, tools=tools)
    tool_calls = response.choices[0].message.tool_calls

    if tool_calls:
        available_functions = {
            "get_flight_status": get_flight_status,
        }
        
        for tool_call in tool_calls:
            function_name = tool_call.function.name
            function_to_call = available_functions[function_name]
            function_args = tool_call.function.arguments
        
            function_response = function_to_call(flight=function_args.get("flight"))
            
            messages.append(
                {
                    "role": "tool",
                    "name": function_name,
                    "content": function_response
                }
            )
            
        final_response = client.chat_completion(messages=messages)
        return final_response
        
    return response


res=chatbot("What's the status of EK226?")

print(res.choices[0].message.content)    

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1892048.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

解锁Diffusion Model: 初识Stable Diffusion、DALL-E、Imagen

------->更多内容&#xff0c;请移步“鲁班秘笈”&#xff01;&#xff01;<------- 扩散模型在生成高质量图像、视频、声音等方面表现突出。它们与物理学中的自然扩散过程相似而得名&#xff0c;自然扩散过程描述了分子如何从高浓度区域移动到低浓度区域。在机器学习的…

【Python】已解决:ModuleNotFoundError: No module named ‘cv2’

文章目录 一、分析问题背景二、可能出错的原因三、错误代码示例四、正确代码示例五、注意事项 已解决&#xff1a;ModuleNotFoundError: No module named ‘cv2’ 一、分析问题背景 在Python编程中&#xff0c;当你尝试导入一个不存在的模块时&#xff0c;会遇到ModuleNotFou…

公有链、私有链与联盟链:区块链技术的多元化应用与比较

引言 区块链技术自2008年比特币白皮书发布以来&#xff0c;迅速发展成为一项具有颠覆性潜力的技术。区块链通过去中心化、不可篡改和透明的方式&#xff0c;提供了一种全新的数据存储和管理方式。起初&#xff0c;区块链主要应用于加密货币&#xff0c;如比特币和以太坊。然而&…

Vue84-Vuex的工作原理与搭建开发环境

一、vuex工作原理 stats&#xff1a;是一个object对象&#xff0c;里面有很多key-value&#xff0c;存放的就是要操作的数据。mutations&#xff1a;是一个object对象&#xff0c;真正去操作stats的人。actions的作用&#xff1a;是一个object对象&#xff0c;当一个动作对应的…

力扣Hot100-24两两交换链表中的节点(三指针)

给你一个链表&#xff0c;两两交换其中相邻的节点&#xff0c;并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题&#xff08;即&#xff0c;只能进行节点交换&#xff09;。 示例 1&#xff1a; 输入&#xff1a;head [1,2,3,4] 输出&#xff1a;[2,1,4…

VirtualBox 虚拟机的网络通过宿主机的网络进行冲浪

虚拟机与宿主机通过桥接模式处在同一个网络中 1.说明2.操作步骤2.1.虚拟机设置网络2.2.手动指定虚拟机的IP 1.说明 A.虚拟机 ubuntu-20.04 B.宿主机网络 Wireless LAN adapter WLAN:Connection-specific DNS Suffix . : lanIPv4 Address. . . . . . . . . . . : 192.168.111…

MYSQL的简易安装

先下载好安装包 官网&#xff1a;https://www.mysql.com 双击运行进入界面 打开之后将左侧的产品移到右侧 点击使用的产品设置路径 之后一直下一步即可 选择主机类型 同时要记住端口号 设置密码 设置系统服务列表中的服务名称 之后一直下一步就可以了 安装完成记得配置环…

AUTOSAR NvM模块(七)

NvM工具配置demo 一切block的配置根据自己的需求&#xff01; NvMBlockDescriptor NvM Common MemIf General FeeBlockConfiguration FeeGeneral

CASS中按指定距离和方向移动图形

1、绘制一个图形 打开软件&#xff0c;随意绘制一个矩形&#xff0c;并量取左下角点的坐标值&#xff0c;具体如下&#xff1a; 2、按距离移动原理讲解 例1&#xff1a;将图形沿着y轴负方向移动100米&#xff0c;如何实现&#xff1f; 如上图所示&#xff0c;测绘中的坐标系…

【FPGA】STA静态时序分析

文章目录 一.定义二.分类1. 静态时序分析2. 静态时序分析 三. 概念四. 时间余量1.场景2.建立时间余量3.保持时间余量 一.定义 时序分析:检查电路是否满足时序要求&#xff1b; 二.分类 1. 静态时序分析 STA,遍历所有的时序路径&#xff0c;根据时序库&#xff08;.lib文件&…

基于Java中的SSM框架实现物流管理系统项目【项目源码+论文说明】

基于Java中的SSM框架实现物流管理系统演示 摘要 企业的发展离不开物流的运输&#xff0c;在一个大型的企业中&#xff0c;商品的生产和建设&#xff0c;推广只是前期的一些工作&#xff0c;在后期的商品销售和物流方面的建立&#xff0c;才能让一个企业得到大力的发展。 企业…

源代码层面分析Appium-inspector工作原理

Appium-inspector功能 Appium Inspector 基于 Appium 框架&#xff0c;Appium 是一个开源工具&#xff0c;用于自动化移动应用&#xff08;iOS 和 Android&#xff09;和桌面应用&#xff08;Windows 和 Mac&#xff09;。Appium 采用了客户端-服务器架构&#xff0c;允许用户通…

BNO055 智能传感器

型号简介 BNO055是博世&#xff08;bosch-sensortec&#xff09;的一款系统级封装 (SiP) 解决方案&#xff0c;集成了三轴 14 位加速度计、精确的闭环三轴 16 位陀螺仪、三轴地磁传感器和运行 BSX3.0 FusionLib 软件的 32 位微控制器。这款智能传感器比同类解决方案小得多。通过…

JAVA 获取客户端信息工具类

获取客户端信息工具类 import com.wangyao.common.constant.ConstantNumeral; import jakarta.annotation.Nullable; import jakarta.servlet.http.HttpServletRequest; import jakarta.validation.constraints.NotNull; import lombok.extern.slf4j.Slf4j;import java.io.IOEx…

一文了解微服务架构路线

为什么选择微服务架构&#xff1f; 众所周知&#xff0c;单体应用程序&#xff0c;由于其种种不足&#xff0c;几乎不支持敏捷方法。如果你想为一个大型或复杂的业务创建一个软件项目&#xff0c;最好从微服务架构开始。 微服务架构是一种灵活的架构&#xff0c;可以显著性地提…

阿里云 ECS 服务器的安全组设置

阿里云 ECS 服务器的安全组设置 缘由安全组多个安全组各司其职一些常见的IP段百度 IP 段华为云 IP 段搜狗蜘蛛 IP 段阿里云 IP 段 。。。 缘由 最近公司规模缩减&#xff0c;原有的托管在 IDC 机房的服务器&#xff0c;都被处理掉了&#xff0c;所有代码都迁移到了阿里云的云服…

手机图库照片删除如何找回?别再捶胸顿足,用这3招恢复

手机和电脑中的照片越来越多&#xff0c;人们在浏览照片时&#xff0c;可能会不小心删除一些重要的照片。这些照片可能是旅行中的风景照、与家人朋友的合影&#xff0c;甚至是一些具有纪念意义的照片。一旦删除&#xff0c;我们可能就无法找回这些照片了。因此&#xff0c;图库…

【postgresql】索引

见的索引类型&#xff1a; B-tree 索引&#xff1a;这是最常用的索引类型&#xff0c;适用于大多数查询。B-tree索引可以高效地处理范围查询。 Hash 索引&#xff1a;适用于等值查询&#xff0c;但不支持范围查询。 GiST 索引&#xff1a;通用搜索树&#xff08;GiST&#xf…

ELISA实验前,需要做好哪些准备?

进行ELISA试剂盒实验前&#xff0c;需要进行周密的准备工作以确保实验的顺利进行和实验的准确性。那么&#xff0c;具体应该做哪些准备呢&#xff1f;欣博盛生物为您总结了一些关键的准备工作步骤&#xff1a; 1. 阅读说明书 仔细阅读ELISA试剂盒的说明书&#xff0c;了解试剂…

CANoe的capl调用Qt制作的dll

闲谈 因为Qt封装了很多个人感觉很好用的库&#xff0c;所以一直想通过CAPL去调用Qt实现一些功能&#xff0c;但是一直没机会&#xff08;网络上也没看到这方面的教程&#xff09;&#xff0c;这次自己用了两天&#xff0c;踩了很多坑&#xff0c;终于是做成了一个初步的调用方…