评测腾讯混元大模型,python调用混元生文接口

2024-09-13 17:44:02 浏览数 (1)

想丰富腾讯混元python sdk 调用混元大模型的实例。下面介绍python实现混元生文接口ChatCompletions实例。

开发环境介绍

python 3.9

1、 pip 安装 tencentcloud-sdk-python,云 API 3.0 提供了配套的开发工具集(SDK),能更方便的调用 API。

代码语言:python代码运行次数:0复制
pip install -i https://mirrors.tencent.com/pypi/simple/ --upgrade tencentcloud-sdk-python

2、 获取API密钥

访问 https://console.cloud.tencent.com/hunyuan/start

例子一:说明 system prompt 的使用方法

代码语言:python代码运行次数:0复制
import json
import os
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.hunyuan.v20230901 import hunyuan_client, models


try:
    # 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
    cred = credential.Credential(
        os.environ.get("TENCENTCLOUD_SECRET_ID"),
        os.environ.get("TENCENTCLOUD_SECRET_KEY"))

    cpf = ClientProfile()
    # 预先建立连接可以降低访问延迟
    cpf.httpProfile.pre_conn_pool_size = 3
    client = hunyuan_client.HunyuanClient(cred, "ap-guangzhou", cpf)

    req = models.ChatCompletionsRequest()
    req.Model = "hunyuan-standard"
    msg1 = models.Message()
    msg1.Role = "system"
    msg1.Content = "将英文单词转换为包括中文翻译、英文释义和一个例句的完整解释。请检查所有信息是否准确,并在回答时保持简洁,不需要任何其他反馈。"

    msg2 = models.Message()
    msg2.Role = "user"
    msg2.Content = "nice"

    req.Messages = [msg1, msg2]

    # hunyuan ChatCompletions 同时支持 stream 和非 stream 的情况
    req.Stream = True
    resp = client.ChatCompletions(req)

    full_content = ""
    if req.Stream:  # stream 示例
        for event in resp:
            print(event["data"])
            data = json.loads(event['data'])
            for choice in data['Choices']:
                full_content  = choice['Delta']['Content']
    else:  # 非 stream 示例
        # 通过 Stream=False 参数来指定非 stream 协议, 一次性拿到结果
        full_content = resp.Choices[0].Message.Content

    print(full_content)

except TencentCloudSDKException as err:
    print(err)

运行结果如下

代码语言:python代码运行次数:0复制
{"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"好的"},"FinishReason":""}],"Created":1726218692,"Id":"54472835-52e0-4567-882f-975098478c96","Usage":{"PromptTokens":40,"CompletionTokens":1,"TotalTokens":41}}
{"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":""},"FinishReason":"stop"}],"Created":1726218692,"Id":"54472835-52e0-4567-882f-975098478c96","Usage":{"PromptTokens":40,"CompletionTokens":1,"TotalTokens":41}}
好的

例子二:hunyuan-functioncall模型如何用流式方式调用接口

代码语言:python代码运行次数:0复制
import json
import os
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.hunyuan.v20230901 import hunyuan_client, models

try:
    # 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
    cred = credential.Credential(
        os.environ.get("TENCENTCLOUD_SECRET_ID"),
        os.environ.get("TENCENTCLOUD_SECRET_KEY"))

    cpf = ClientProfile()
    # 预先建立连接可以降低访问延迟
    cpf.httpProfile.pre_conn_pool_size = 3
    client = hunyuan_client.HunyuanClient(cred, "ap-guangzhou", cpf)

    req = models.ChatCompletionsRequest()
    req.Model = "hunyuan-functioncall"
    msg = models.Message()
    msg.Role = "user"
    msg.Content = "北京和深圳今天天气如何"

    tool = models.Tool()
    tool.Type = "function"
    toolFunction = models.ToolFunction()
    toolFunction.Name = "get_current_weather"
    parameters = {
        "type": "object",
        "properties": {
            "location": {
                "type": "string",
                "description": "城市名称"
            },
            "unit": {
                "type": "string",
                "enum": ["celsius", "fahrenheit"]
            }
        },
        "required": ["location"]
    }
    toolFunction.Parameters = json.dumps(parameters)
    tool.Function = toolFunction

    req.Messages = [msg]
    req.Tools = [tool]


    # hunyuan ChatCompletions 同时支持 stream 和非 stream 的情况
    req.Stream = True
    resp = client.ChatCompletions(req)

    full_content = ""
    if req.Stream:  # stream 示例
        for event in resp:
            print(event["data"])
            data = json.loads(event['data'])
            for choice in data['Choices']:
                full_content  = choice['Delta']['Content']
    else:  # 非 stream 示例
        # 通过 Stream=False 参数来指定非 stream 协议, 一次性拿到结果
        full_content = resp.Choices[0].Message.Content

    print(full_content)

except TencentCloudSDKException as err:
    print(err)

运行结果如下

代码语言:python代码运行次数:0复制
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"","ToolCalls":[{"Id":"call_cri058k2c3m3k4q668lg","Type":"function","Function":{"Name":"get_current_weather","Arguments":""}}]},"FinishReason":""}],"Created":1726218914,"Id":"9f8a5235-ebff-44ca-9600-18a51cc405e0","Usage":{"PromptTokens":6,"CompletionTokens":0,"TotalTokens":6}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"","ToolCalls":[{"Id":"call_cri058k2c3m3k4q668lg","Type":"function","Function":{"Name":"","Arguments":"{"location":["北京","深圳"],"unit":"celsius"}"}}]},"FinishReason":""}],"Created":1726218914,"Id":"9f8a5235-ebff-44ca-9600-18a51cc405e0","Usage":{"PromptTokens":6,"CompletionTokens":0,"TotalTokens":6}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"调用get_current_weather工具,分别查询北京和深圳的天气。ntnt用户想要知道北京和深圳今天的天气情况。需要调用天气查询工具来获取北京和深圳的当前天气。"},"FinishReason":""}],"Created":1726218914,"Id":"9f8a5235-ebff-44ca-9600-18a51cc405e0","Usage":{"PromptTokens":6,"CompletionTokens":42,"TotalTokens":48}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":""},"FinishReason":"tool_calls"}],"Created":1726218914,"Id":"9f8a5235-ebff-44ca-9600-18a51cc405e0","Usage":{"PromptTokens":6,"CompletionTokens":42,"TotalTokens":48}}
调用get_current_weather工具,分别查询北京和深圳的天气。
	
	用户想要知道北京和深圳今天的天气情况。需要调用天气查询工具来获取北京和深圳的当前天气。

例子三:hunyuan-functioncall模型多轮对话如何调用接口,调用工具

代码语言:python代码运行次数:0复制
import json
import os
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.hunyuan.v20230901 import hunyuan_client, models

try:
    # 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
    cred = credential.Credential(
        os.environ.get("TENCENTCLOUD_SECRET_ID"),
        os.environ.get("TENCENTCLOUD_SECRET_KEY"))

    cpf = ClientProfile()
    # 预先建立连接可以降低访问延迟
    cpf.httpProfile.pre_conn_pool_size = 3
    client = hunyuan_client.HunyuanClient(cred, "ap-guangzhou", cpf)

    req = models.ChatCompletionsRequest()
    req.Model = "hunyuan-functioncall"

    msg1 = models.Message()
    msg1.Role = "user"
    msg1.Content = "北京和深圳今天天气如何"

    msg2 = models.Message()
    msg2.Role = "assistant"
    msg2.Content = "使用get_current_weather工具来获取北京和深圳的当前天气。ntnt用户想要知道北京和深圳今天的天气情况。用户提供了两个城市名称,但没有指定温度单位,根据常识,默认用户需要的是摄氏度。"

    toolCall = models.ToolCall()
    toolCall.Id = "call_cri058k2c3m3k4q668lg"
    toolCall.Type = "function"
    toolCallFunction = models.ToolCallFunction()
    toolCallFunction.Name = "get_current_weather"
    toolCallFunction.Arguments = json.dumps({"location": ["北京","深圳"], "unit": "celsius"})
    toolCall.Function = toolCallFunction
    msg2.ToolCalls = [toolCall]

    msg3 = models.Message()
    msg3.Role = "tool"
    msg3.ToolCallId = "call_cri058k2c3m3k4q668lg"
    msg3.Content = json.dumps([{"location": "北京", "temperature": 35, "wind": "西南风", "condition": "暴雨"},
                               {"location": "深圳", "temperature": 20, "wind": "西北风", "condition": "晴"}])

    tool = models.Tool()
    tool.Type = "function"
    toolFunction = models.ToolFunction()
    toolFunction.Name = "get_current_weather"
    parameters = {
        "type": "object",
        "properties": {
            "location": {
                "type": "string",
                "description": "城市名称"
            },
            "unit": {
                "type": "string",
                "enum": ["celsius", "fahrenheit"]
            }
        },
        "required": ["location"]
    }
    toolFunction.Parameters = json.dumps(parameters)
    tool.Function = toolFunction

    req.Messages = [msg1, msg2, msg3]
    req.Tools = [tool]

    # hunyuan ChatCompletions 同时支持 stream 和非 stream 的情况
    req.Stream = True
    resp = client.ChatCompletions(req)

    full_content = ""
    if req.Stream:  # stream 示例
        for event in resp:
            print(event["data"])
            data = json.loads(event['data'])
            for choice in data['Choices']:
                full_content  = choice['Delta']['Content']
    else:  # 非 stream 示例
        # 通过 Stream=False 参数来指定非 stream 协议, 一次性拿到结果
        full_content = resp.Choices[0].Message.Content

    print(full_content)

except TencentCloudSDKException as err:
    print(err)

运行结果如下

代码语言:python代码运行次数:0复制
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"北京"},"FinishReason":""}],"Created":1726219892,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":1,"TotalTokens":145}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"今天的"},"FinishReason":""}],"Created":1726219892,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":2,"TotalTokens":146}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"天气"},"FinishReason":""}],"Created":1726219892,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":3,"TotalTokens":147}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"情况"},"FinishReason":""}],"Created":1726219892,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":4,"TotalTokens":148}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"如下"},"FinishReason":""}],"Created":1726219892,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":5,"TotalTokens":149}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":":"},"FinishReason":""}],"Created":1726219892,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":6,"TotalTokens":150}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"n"},"FinishReason":""}],"Created":1726219892,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":7,"TotalTokens":151}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"-"},"FinishReason":""}],"Created":1726219892,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":8,"TotalTokens":152}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":" 温度"},"FinishReason":""}],"Created":1726219892,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":9,"TotalTokens":153}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":":"},"FinishReason":""}],"Created":1726219892,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":10,"TotalTokens":154}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"3"},"FinishReason":""}],"Created":1726219892,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":11,"TotalTokens":155}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"5"},"FinishReason":""}],"Created":1726219892,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":12,"TotalTokens":156}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"℃"},"FinishReason":""}],"Created":1726219892,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":13,"TotalTokens":157}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"n"},"FinishReason":""}],"Created":1726219892,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":14,"TotalTokens":158}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"-"},"FinishReason":""}],"Created":1726219892,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":15,"TotalTokens":159}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":" 风"},"FinishReason":""}],"Created":1726219892,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":16,"TotalTokens":160}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"向"},"FinishReason":""}],"Created":1726219892,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":17,"TotalTokens":161}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":":"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":18,"TotalTokens":162}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"西南"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":19,"TotalTokens":163}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"风"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":20,"TotalTokens":164}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"n"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":21,"TotalTokens":165}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"-"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":22,"TotalTokens":166}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":" 天气"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":23,"TotalTokens":167}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"状况"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":24,"TotalTokens":168}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":":"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":25,"TotalTokens":169}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"晴"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":26,"TotalTokens":170}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"n"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":27,"TotalTokens":171}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"n"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":28,"TotalTokens":172}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"深圳"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":29,"TotalTokens":173}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"今天的"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":30,"TotalTokens":174}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"天气"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":31,"TotalTokens":175}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"情况"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":32,"TotalTokens":176}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"如下"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":33,"TotalTokens":177}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":":"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":34,"TotalTokens":178}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"n"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":35,"TotalTokens":179}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"-"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":36,"TotalTokens":180}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":" 温度"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":37,"TotalTokens":181}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":":"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":38,"TotalTokens":182}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"2"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":39,"TotalTokens":183}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"0"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":40,"TotalTokens":184}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"℃"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":41,"TotalTokens":185}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"n"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":42,"TotalTokens":186}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"-"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":43,"TotalTokens":187}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":" 风"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":44,"TotalTokens":188}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"向"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":45,"TotalTokens":189}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":":"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":46,"TotalTokens":190}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"东北"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":47,"TotalTokens":191}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"风"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":48,"TotalTokens":192}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"n"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":49,"TotalTokens":193}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"-"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":50,"TotalTokens":194}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":" 天气"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":51,"TotalTokens":195}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"状况"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":52,"TotalTokens":196}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":":"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":53,"TotalTokens":197}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"阴"},"FinishReason":""}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":54,"TotalTokens":198}}
 {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":""},"FinishReason":"stop"}],"Created":1726219893,"Id":"144a43e5-1d79-42ee-9f1f-60eb4ea640b1","Usage":{"PromptTokens":144,"CompletionTokens":54,"TotalTokens":198}}
北京今天的天气情况如下:
- 温度:35℃
- 风向:西南风
- 天气状况:晴

深圳今天的天气情况如下:
- 温度:20℃
- 风向:东北风
- 天气状况:阴

结论

ChatCompletions接口的示例一和示例二返回正常,可以实现系统提示问答、多轮会话问答。示例三多轮问答中调用查询天气工具时,混元大模型回答的有问题。

工具查询的天气情况是:

北京今天的天气情况如下:

  • 温度:35℃
  • 风向:西南风
  • 天气状况:暴雨

深圳今天的天气情况如下:

  • 温度:20℃
  • 风向:西北风
  • 天气状况:晴

大模型回答的结果是:

北京今天的天气情况如下:

  • 温度:35℃
  • 风向:西南风
  • 天气状况:晴

深圳今天的天气情况如下:

  • 温度:20℃
  • 风向:东北风
  • 天气状况:阴

不知道是不是我写的有问题啊,希望小伙伴们帮忙指导品鉴,希望社区人员能帮忙解答下。

0 人点赞