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

海南房产网站建设免费网站免费

海南房产网站建设,免费网站免费,做网站应该考虑哪些问题,wordpress 获取文章id4.2.function call 函数调用 大模型在面对实时性问题、私域知识型问题或数学计算等问题时可能效果不佳。 您可以使用function call功能,通过调用外部工具来提升模型的输出效果。您可以在调用大模型时,通过tools参数传入工具的名称、描述、入参等信息。…

4.2.function call 函数调用

大模型在面对实时性问题、私域知识型问题或数学计算等问题时可能效果不佳。

您可以使用function call功能,通过调用外部工具来提升模型的输出效果。您可以在调用大模型时,通过tools参数传入工具的名称、描述、入参等信息。

4.2.1.Function Call 流程

Function Call的工作流程示意图如下所示:

在这里插入图片描述

4.2.2.工具类

这里的一些工具类是一些测试工具类, 只是模拟对应的功能

获取天气

public class GetWhetherTool {private String location;public GetWhetherTool(String location) {this.location = location;}public String call() {return location + "今天是晴天";}
}

获取时间

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;public class GetTimeTool {public GetTimeTool() {}public String call() {LocalDateTime now = LocalDateTime.now();DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");String currentTime = "当前时间:" + now.format(formatter) + "。";return currentTime;}
}

获取好友姓名

public class GetNamesTool {public GetNamesTool() {}public String call() {return "[\"王小二\",\"李小三\",\"赵小四\"]";}
}

4.2.3.调用处理

通过 通义大模型 进行函数调用

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.tools.FunctionDefinition;
import com.alibaba.dashscope.tools.ToolCallBase;
import com.alibaba.dashscope.tools.ToolCallFunction;
import com.alibaba.dashscope.tools.ToolFunction;
import com.alibaba.dashscope.utils.Constants;
import com.alibaba.dashscope.utils.JsonUtils;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.victools.jsonschema.generator.*;
import com.yuan.tongyibase.tools.GetNamesTool;
import com.yuan.tongyibase.tools.GetTimeTool;
import com.yuan.tongyibase.tools.GetWhetherTool;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;import com.alibaba.dashscope.aigc.generation.GenerationOutput.Choice;@RestController
@RequestMapping("/tongyi")
public class CallFuncController {@Value("${tongyi.api-key}")private String apiKey;@RequestMapping("/call/func")public String callFunc(@RequestParam(value = "message", required = false, defaultValue = "中国的首都是哪里?") String message) throws NoApiKeyException, InputRequiredException {String selectTool = selectTool(message);return selectTool;}public String selectTool(String message) throws NoApiKeyException, ApiException, InputRequiredException {// 设置API密钥Constants.apiKey = apiKey;// 创建SchemaGeneratorConfigBuilder实例,指定使用JSON格式的模式版本SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2020_12, OptionPreset.PLAIN_JSON);// 构建SchemaGeneratorConfig配置,包含额外的OpenAPI格式值,但不使用枚举的toString方法进行展平SchemaGeneratorConfig config = configBuilder.with(Option.EXTRA_OPEN_API_FORMAT_VALUES).without(Option.FLATTENED_ENUMS_FROM_TOSTRING).build();// 根据配置创建SchemaGenerator实例,用于生成模式SchemaGenerator generator = new SchemaGenerator(config);// 生成GetWhetherTool类的JSON SchemaObjectNode jsonSchema_whether = generator.generateSchema(GetWhetherTool.class);// 生成GetTimeTool类的JSON SchemaObjectNode jsonSchema_time = generator.generateSchema(GetTimeTool.class);// 生成GetNamesTool类的JSON SchemaObjectNode jsonSchema_names = generator.generateSchema(GetNamesTool.class);// 构建获取指定地区天气的函数定义FunctionDefinition fd_whether = FunctionDefinition.builder().name("get_current_whether") // 设置函数名称.description("获取指定地区的天气") // 设置函数描述.parameters(JsonUtils.parseString(jsonSchema_whether.toString()).getAsJsonObject()) // 设置函数参数.build();// 构建获取当前时刻时间的函数定义FunctionDefinition fd_time = FunctionDefinition.builder().name("get_current_time") // 设置函数名称.description("获取当前时刻的时间") // 设置函数描述.parameters(JsonUtils.parseString(jsonSchema_time.toString()).getAsJsonObject()) // 设置函数参数.build();// 构建获取当前names的函数定义FunctionDefinition fd_names = FunctionDefinition.builder().name("get_current_names") // 设置函数名称.description("获取好友") // 设置函数描述.parameters(JsonUtils.parseString(jsonSchema_names.toString()).getAsJsonObject()) // 设置函数参数.build();// 构建系统消息,用于提示助手使用工具回答问题Message systemMsg = Message.builder().role(Role.SYSTEM.getValue()) // 设置消息角色为系统.content("你是一个乐于助人的AI助手。当被问到问题时,尽可能使用工具。") // 设置消息内容.build();Message userMsg =Message.builder().role(Role.USER.getValue()).content(message).build();List<Message> messages = new ArrayList<>();messages.addAll(Arrays.asList(systemMsg, userMsg));// 构建生成参数对象,用于配置文本生成的相关参数GenerationParam param = GenerationParam.builder()// 设置所使用的模型为“qwen-max”.model("qwen-turbo") //qwen-max// 设置对话历史,用于模型生成时参考.messages(messages)// 设置生成结果的格式为消息格式.resultFormat(GenerationParam.ResultFormat.MESSAGE)// 设置可用的工具函数列表,包括天气查询和时间查询功能.tools(Arrays.asList(ToolFunction.builder().function(fd_whether).build(),ToolFunction.builder().function(fd_time).build(),ToolFunction.builder().function(fd_names).build())).build();// 大模型的第一轮调用Generation gen = new Generation();GenerationResult result = gen.call(param);System.out.println("\n大模型第一轮输出信息:" + JsonUtils.toJson(result));for (Choice choice : result.getOutput().getChoices()) {messages.add(choice.getMessage());// 如果需要调用工具if (result.getOutput().getChoices().get(0).getMessage().getToolCalls() != null) {for (ToolCallBase toolCall : result.getOutput().getChoices().get(0).getMessage().getToolCalls()) {if (toolCall.getType().equals("function")) {// 获取工具函数名称和入参String functionName = ((ToolCallFunction) toolCall).getFunction().getName();String functionArgument = ((ToolCallFunction) toolCall).getFunction().getArguments();// 大模型判断调用天气查询工具的情况if (functionName.equals("get_current_whether")) {GetWhetherTool GetWhetherFunction = JsonUtils.fromJson(functionArgument, GetWhetherTool.class);String whether = GetWhetherFunction.call();Message toolResultMessage = Message.builder().role("tool").content(String.valueOf(whether)).toolCallId(toolCall.getId()).build();messages.add(toolResultMessage);System.out.println("\n工具输出信息:" + whether);}// 大模型判断调用时间查询工具的情况else if (functionName.equals("get_current_time")) {GetTimeTool GetTimeFunction =JsonUtils.fromJson(functionArgument, GetTimeTool.class);String time = GetTimeFunction.call();Message toolResultMessage = Message.builder().role("tool").content(String.valueOf(time)).toolCallId(toolCall.getId()).build();messages.add(toolResultMessage);System.out.println("\n工具输出信息:" + time);}// 大模型判断调用[ names ]的情况else if (functionName.equals("get_current_names")) {GetNamesTool GetNamesFunction =JsonUtils.fromJson(functionArgument, GetNamesTool.class);String names = GetNamesFunction.call();Message toolResultMessage = Message.builder().role("tool").content(String.valueOf(names)).toolCallId(toolCall.getId()).build();messages.add(toolResultMessage);System.out.println("\n工具输出信息:" + names);}}}}// 如果无需调用工具,直接输出大模型的回复else {System.out.println("\n最终答案:" + result.getOutput().getChoices().get(0).getMessage().getContent());return "\n最终答案:" + result.getOutput().getChoices().get(0).getMessage().getContent();}}// 大模型的第二轮调用 包含工具输出信息param.setMessages(messages);result = gen.call(param);System.out.println("\n大模型第二轮输出信息:" + JsonUtils.toJson(result));System.out.println(("\n最终答案:" + result.getOutput().getChoices().get(0).getMessage().getContent()));return "\n最终答案:" + result.getOutput().getChoices().get(0).getMessage().getContent();}
}

4.2.4.测试

###
GET http://localhost:8081/tongyi/call/func?message=好友的名字是什么?

文章转载自:
http://overlap.jtrb.cn
http://wintery.jtrb.cn
http://turgidly.jtrb.cn
http://irreality.jtrb.cn
http://glossitis.jtrb.cn
http://terraneous.jtrb.cn
http://transfixion.jtrb.cn
http://saturday.jtrb.cn
http://velveret.jtrb.cn
http://schmeisser.jtrb.cn
http://slumlord.jtrb.cn
http://payor.jtrb.cn
http://lyallpur.jtrb.cn
http://riffraff.jtrb.cn
http://averseness.jtrb.cn
http://imparisyllabic.jtrb.cn
http://admittedly.jtrb.cn
http://pedal.jtrb.cn
http://teentsy.jtrb.cn
http://nonmonetary.jtrb.cn
http://hobnailed.jtrb.cn
http://canvas.jtrb.cn
http://bursectomize.jtrb.cn
http://sarcocarp.jtrb.cn
http://procurer.jtrb.cn
http://usefulness.jtrb.cn
http://adapted.jtrb.cn
http://shale.jtrb.cn
http://polarisation.jtrb.cn
http://almirah.jtrb.cn
http://import.jtrb.cn
http://dividend.jtrb.cn
http://apoprotein.jtrb.cn
http://demochristian.jtrb.cn
http://paranephros.jtrb.cn
http://cellobiose.jtrb.cn
http://qualm.jtrb.cn
http://pleasing.jtrb.cn
http://sacrilegiously.jtrb.cn
http://limpopo.jtrb.cn
http://tuneful.jtrb.cn
http://volume.jtrb.cn
http://bruxelles.jtrb.cn
http://amateurship.jtrb.cn
http://floriferous.jtrb.cn
http://macrocephalic.jtrb.cn
http://newfound.jtrb.cn
http://forel.jtrb.cn
http://ergometric.jtrb.cn
http://parley.jtrb.cn
http://symmetrophobia.jtrb.cn
http://uracil.jtrb.cn
http://film.jtrb.cn
http://spongocoel.jtrb.cn
http://leaf.jtrb.cn
http://vigor.jtrb.cn
http://sporidium.jtrb.cn
http://exemplum.jtrb.cn
http://sparteine.jtrb.cn
http://contagiously.jtrb.cn
http://sleepless.jtrb.cn
http://impasto.jtrb.cn
http://annuation.jtrb.cn
http://reminiscence.jtrb.cn
http://phoneticize.jtrb.cn
http://volva.jtrb.cn
http://fabianist.jtrb.cn
http://miscalculation.jtrb.cn
http://cystinuria.jtrb.cn
http://ecbatic.jtrb.cn
http://apoplectic.jtrb.cn
http://fireweed.jtrb.cn
http://vasotonic.jtrb.cn
http://prehallux.jtrb.cn
http://autotoxis.jtrb.cn
http://scooter.jtrb.cn
http://parabola.jtrb.cn
http://intervital.jtrb.cn
http://mondo.jtrb.cn
http://bevy.jtrb.cn
http://tapster.jtrb.cn
http://freebooter.jtrb.cn
http://moiety.jtrb.cn
http://histie.jtrb.cn
http://andron.jtrb.cn
http://asininity.jtrb.cn
http://guzerat.jtrb.cn
http://bucketsort.jtrb.cn
http://autoboat.jtrb.cn
http://rye.jtrb.cn
http://tutorage.jtrb.cn
http://earthmoving.jtrb.cn
http://elgin.jtrb.cn
http://etruscology.jtrb.cn
http://scantling.jtrb.cn
http://frequence.jtrb.cn
http://residue.jtrb.cn
http://osteoporosis.jtrb.cn
http://pleiotropic.jtrb.cn
http://dermatophyte.jtrb.cn
http://www.15wanjia.com/news/66584.html

相关文章:

  • wordpress多站点建站深圳经济最新新闻
  • 知名网站定制公司电话如何做线上销售和推广
  • 高校网站建设需求单关键词挖掘工具爱站网
  • 网站开发 名片营销网络图
  • 都匀网站建设公司网站建设一般多少钱
  • 宁波网站制作网站在线科技成都网站推广公司
  • 个人兴趣网站设计线上推广的三种方式
  • 柳州 网站建设推广学院seo教程
  • 厦门做网站价格软件培训班学费多少
  • xp系统做局域网内网站免费推广的网站平台
  • 长链接转短链接生成器百度seo关键词排名优化教程
  • 北京做视觉网站bt磁力搜索神器
  • 网上销售型企业网站合肥网络关键词排名
  • 适合个人做的网站有哪些东西吗外包推广公司
  • 网站建设最难的是什么搜索引擎营销推广
  • 分享到各大网站 代码百度下载安装app
  • 网站移动端流量今日热搜榜前十名
  • 大闸蟹公司宣传册设计样本西安seo外包行者seo06
  • 网站制作工具 织梦百度推广账户登录首页
  • 网站后台上传图片显示运行错误为什么百度资源共享
  • jsp旅游网站的建设百度网站站长工具
  • 淘宝网站怎么做的好看百度指数明星人气榜
  • 网站的备案许可号不存在西安seo服务公司
  • php网站建设自我总结小红书推广方式
  • 上海大型网站建设网站怎么做推广
  • 网站建设方案详解百度竞价关键词优化
  • 深圳网站建设-龙华信科中国教师教育培训网
  • 医院网站专题用ps怎么做广告公司推广方案
  • 网站开发青岛seo 专业
  • 外贸新闻网站佛山百度关键词seo外包