前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >元器智能体API调用

元器智能体API调用

原创
作者头像
孟斯特
发布2024-08-25 09:20:43
20
发布2024-08-25 09:20:43
举报
文章被收录于专栏:Go学习

1. 什么是元器?

“腾讯元器”是基于腾讯混元大模型的一站式智能体制作平台,支持通过下述能力对大模型进行增强:

  • 提示词,包含详细设定(system prompt),开场白,建议引导问题。
  • 插件(外部API),目前支持勾选多个插件。官方插件包含网页解析、混元生图、图片理解等,也支持用户自定义插件。
  • 知识库,当前版本支持doc、docx、txt、PDF四种格式。
  • 工作流,一种“流程图”式的低代码编辑工具,可以用来做一个“高级版”插件。在工作流里,可以任意编排插件、知识库、大模型节点的工作顺序和调用传参,从而精确控制智能体中部分任务的运行逻辑。

通过元器平台制作的智能体,目前支持32k token上下文长度(某次回答过程中的提示词+机器回答的token长度,一个token约为1.8个中文字符)。工作流的超时运行时间为240s。智能体的回复上限时间是240s。

2. 发布元器智能体

调用API需要先创建一个对应的智能体。关于智能体的创建,可以参考官方文档

创建智能体后,还需要进行发布。发布时,可以指定智能体的公开范围:

  • 所有人可用:该智能体会展示在腾讯元器和元宝App内,可以被用户通过站内搜索搜到;
  • 仅通过分享链接进入者可用:无法被元器和元宝的搜索搜到该智能体,但是可以通过链接分享给朋友使用;
  • 仅自己可用,只有你的账号自己可用该智能体

发布后,就可以通过API调用该智能体了。

3. 调用智能体API

API接口文档可以从这里找到。

3.1 定义请求参数

代码语言:go
复制
// File 结构体表示文件信息,包含类型和URL
type File struct {
	Type string `json:"type"`
	URL  string `json:"url"`
}

// MessageContent 结构体表示消息内容,包含类型、文本和文件URL
type MessageContent struct {
	Type    string `json:"type"`
	Text    string `json:"text"`
	FileURL File   `json:"file_url"`
}

// RequestMessage 结构体表示请求消息,包含角色和内容列表
type RequestMessage struct {
	Role    string           `json:"role"`
	Content []MessageContent `json:"content"`
}

// Request 结构体表示请求,包含助手ID、用户ID、是否流式传输和消息列表
type Request struct {
	AssistantID string           `json:"assistant_id"`
	UserID      string           `json:"user_id"`
	Stream      bool             `json:"stream"`
	Messages    []RequestMessage `json:"messages"`
}

// Yuanqi 结构体表示Yuanqi对象,包含token
type Yuanqi struct {
	token string
}

// Usage 结构体表示使用情况,包含提示tokens、完成tokens和总tokens
type Usage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	TotalTokens      int `json:"total_tokens"`
}

// Step 结构体表示步骤,包含角色、内容、工具调用ID、工具调用列表、使用情况和时间成本
type Step struct {
	Role       string     `json:"role"`
	Content    string     `json:"content"`
	ToolCallID string     `json:"tool_call_id"`
	ToolCalls  []ToolCall `json:"tool_calls"`
	Usage      Usage      `json:"usage"`
	TimeCost   int        `json:"time_cost"`
}

// ToolCall 结构体表示工具调用,包含ID、类型和函数
type ToolCall struct {
	ID       string   `json:"id"`
	Type     string   `json:"type"`
	Function Function `json:"function"`
}

// Function 结构体表示函数,包含名称、描述、类型和参数
type Function struct {
	Name      string `json:"name"`
	Desc      string `json:"desc"`
	Type      string `json:"type"`
	Arguments string `json:"arguments"`
}

// ResponseMessage 结构体表示响应消息,包含角色、内容和步骤列表
type ResponseMessage struct {
	Role    string `json:"role"`
	Content string `json:"content"`
	Steps   []Step `json:"steps"`
}

// Delta 结构体表示增量,包含角色、内容、工具调用ID、工具调用列表和时间成本
type Delta struct {
	Role       string     `json:"role"`
	Content    string     `json:"content"`
	ToolCallID string     `json:"tool_call_id"`
	ToolCalls  []ToolCall `json:"tool_calls"`
	TimeCost   int        `json:"time_cost"`
}

// Choice 结构体表示选择,包含索引、完成原因、审核级别、消息和增量
type Choice struct {
	Index           int             `json:"index"`
	FinishReason    string          `json:"finish_reason"`
	ModerationLevel string          `json:"moderation_level"`
	Message         ResponseMessage `json:"message"`
	Delta           Delta           `json:"delta"`
}

// Response 结构体表示响应,包含ID、创建时间、选择列表、助手ID和使用情况
type Response struct {
	ID          string   `json:"id"`
	Created     int      `json:"created"`
	Choices     []Choice `json:"choices"`
	AssistantID string   `json:"assistant_id"`
	Usage       Usage    `json:"usage"`
}

3.2 构建http请求

代码语言:go
复制
// Build 方法构建并返回一个http.Request对象,给定指定的参数
func (y *Yuanqi) Build(ctx context.Context, method, url string, request any) (req *http.Request, err error) {
	// Check if request data is nil.
	if request == nil {
		req, err = http.NewRequestWithContext(ctx, method, url, nil)
	} else {
		//Marshal the request data using Sonic marshaler.
		requestBytes, err := sonic.Marshal(request)
		if err != nil {
			return nil, errors.Wrap(err, "marshal request error")
		}
		//Use the marshaled bytes to create a new request with the specified context, method and URL.
		req, err = http.NewRequestWithContext(ctx, method, url, bytes.NewBuffer(requestBytes))
		if err != nil {
			return nil, errors.Wrap(err, "create http request error")
		}
	}

	// Add the required headers to the request.
	req.Header.Set("Authorization", "Bearer "+y.token)
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("X-source", "openapi")
	return
}

完整代码可以在这里找到。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 什么是元器?
  • 2. 发布元器智能体
  • 3. 调用智能体API
    • 3.1 定义请求参数
      • 3.2 构建http请求
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档