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

企业网站公告怎么做4p营销理论

企业网站公告怎么做,4p营销理论,什么网站可以发布有偿做项目,免费网站下载直播软件在当今AI技术飞速发展的背景下,大语言模型如Qwen2和GLM-4凭借其强大的语言处理能力,在诸多领域展现出了巨大的潜力。然而,大模型并非全知全能,它们在处理特定任务时,尤其是在需要与外部系统交互或执行具体功能时&#…

在当今AI技术飞速发展的背景下,大语言模型如Qwen2和GLM-4凭借其强大的语言处理能力,在诸多领域展现出了巨大的潜力。然而,大模型并非全知全能,它们在处理特定任务时,尤其是在需要与外部系统交互或执行具体功能时,会遇到一定的局限性。这主要是因为大模型通常被设计为封闭的文本生成系统,缺乏直接调用外部工具或API的能力。这种局限性凸显了工具调用在实际应用中的必要性,它能够扩展模型的功能边界,使其能够在真实世界场景中执行更加复杂和具体的操作。

工具调用的必要性

尽管大模型在自然语言理解和生成上取得了显著进步,但它们往往受限于训练数据的内容,无法直接访问网络资源、执行代码或操作数据库等。这意味着在解决实际问题时,模型可能无法提供直接、即时且准确的解决方案,尤其是那些需要实时数据处理或特定功能执行的任务。因此,通过工具调用来增强大模型的功能,成为提升其实用性和灵活性的关键。

在此背景下,ChatGLM3以及最近的GLM-4原生就已经支持了工具调用,这就非常方便,通过直接与外部工具交互,减少了中间环节,提高了响应速度和效率。

tools = [{"name": "track","description": "追踪指定股票的实时价格","parameters": {"type": "object","properties": {"symbol": {"description": "需要追踪的股票代码"}},"required": ['symbol']}},{"name": "text-to-speech","description": "将文本转换为语音","parameters": {"type": "object","properties": {"text": {"description": "需要转换成语音的文本"},"voice": {"description": "要使用的语音类型(男声、女声等)"},"speed": {"description": "语音的速度(快、中等、慢等)"}},"required": ['text']}}
]
system_info = {"role": "system", "content": "Answer the following questions as best as you can. You have access to the following tools:", "tools": tools}

但是Qwen1.5以及Qwen2并不具备原生的工具调用功能,得借助于其Qwen-Agent框架或者langChain框架。那不借助Python框架,我就要使用Java实现该怎么做呢?

使用Java实现Qwen2工具调用

首先,我们需要自定义两个注解FunctionDef​和FunctionParam

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface FunctionDef {/*** 函数名称* @return 函数名称*/String name() default "";/*** 函数描述* @return 函数描述*/String description();
}@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface FunctionParam {/*** 参数名称* @return 参数名称*/String name();/*** 参数描述* @return 参数描述*/String description();/*** 参数枚举* @return 参数枚举*/String[] enums() default {};/*** 是否必填* @return 必填*/boolean required() default false;
}

然后,我们可以根据自己的需求,创建几个工具插件。下面是我创建的一个查询天气的插件:

public class WeatherTool {/*** 查询天气* @param city 城市* @return 天气信息*/@FunctionDef(name = "getWeatherInfo", description = "get the weather info")public static String getWeatherInfo(@FunctionParam(name = "city", description = "the city name") String city) {if (city == null || city.isEmpty()) {throw new IllegalArgumentException("City name must not be null or empty");}OkHttpClient client = new OkHttpClient.Builder().connectTimeout(60, TimeUnit.SECONDS).writeTimeout(60, TimeUnit.SECONDS).readTimeout(60, TimeUnit.SECONDS).build();try {Map<String, String> headers = new HashMap<>(16);headers.put("Content-Type", "application/json");Request.Builder builder = new Request.Builder().url("https://query.asilu.com/weather/baidu/?city="+city);builder.headers(Headers.of(headers));builder.method("GET", null);Request request = builder.build();Response response = client.newCall(request).execute();if (response.isSuccessful()) {ResponseBody responseBody = response.body();JSONObject jsonObject = JSONObject.parseObject(responseBody.string());return jsonObject.toString();} else {throw new OpenAIChatException("Failed with status code %d. messages: %s", response.code(), response.message());}} catch (IOException e) {e.printStackTrace();return "Error encountered while fetching weather data!";}}
}

再然后,我们把所有的工具插件都交给大模型,让它判断要满足用户的提问,应该选择哪个工具插件:

public String getToolResult(String sessionId,String prompt, List<Function> baseTools){String class2Json = buildClass2Json(new BaseFunction());String finalPrompt = String.format("你是一个AI助手,我会给你一个工具对象集合,工具对象包括name(工具名)、description(工具描述)、clazz(工具类名)、parameters(工具参数)。" +"你可以结合工具对象,从用户的问句中提取到关键词,确定要实现用户的任务应该选择哪个工具对象和工具的参数。" +"【工具集合】:%s。" +"【用户提问】:%s?" +"您的响应结果必须为JSON格式,并且不要返回任何不必要的解释,只提供遵循此格式的符合RFC8259的JSON响应。以下是输出必须遵守的JSON Schema实例:‍```%s‍```",JSON.toJSONString(baseTools),prompt,class2Json);String funcParams = chat(sessionId,finalPrompt);funcParams = JSON.parseObject(funcParams, OpenAIChatResponse.class).getChoices().get(0).getMessage().getContent();funcParams = funcParams.substring(funcParams.indexOf("{"), funcParams.lastIndexOf("}")+1);return LoadFunctions.load(JSON.parseObject(funcParams, BaseFunction.class));}

确定哪个工具插件后,再使用LoadFunctions.load加载执行这个工具插件:

public static String load(BaseFunction baseFunction){String className = baseFunction.getClazz();String methodName = baseFunction.getFunctionName();Map<String,String> arg = baseFunction.getParams();List<String> params = new ArrayList<>();String result = "";try {// 加载类Class<?> clazz = Class.forName(className);//可以使用arg.size确定几个参数,我为了演示方便,这里就默认只有一个参数了//int size = arg.size();Method method = clazz.getMethod(methodName,String.class);Parameter[] parameters = method.getParameters();// 如果方法有参数,并且参数类型已知(例如只有一个String类型的参数)for (int i = 0; i < parameters.length; i++){params.add(arg.values().stream().skip(i).findFirst().orElse(null));}// 创建类的实例,如果CarBean有一个无参构造函数Object instance = clazz.newInstance();result = method.invoke(instance,params.toArray()).toString();} catch (ClassNotFoundException e) {LOG.error("类未找到: {}" , className);} catch (NoSuchMethodException e) {LOG.error("找不到方法: {}" , methodName);} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {LOG.error("无法调用方法: {}" , e.getMessage());}return result;}

最后,我们就可以拿到工具执行的结果,然后把工具执行结果直接给到大模型,让它组织语言回答用户提问就可以了

public Flux<String> streamChatWithTools(String sessionId, String prompt, List<Function> baseTools) {//获取工具结果String toolResult = getToolResult(null,prompt, baseTools);LOG.info("工具调用结果为:{}",toolResult);String promptFormat = String.format("基于工具查询的结果:{%s}。请回答:%s?", toolResult, prompt);return streamChat(sessionId, promptFormat);}

到这里,我们就完成了像Qwen2这种没有原生支持Function_call的大模型的工具调用的功能了。

改进优化

在最初的版本中,我们是把普通问答和工具调用的问答分开设计的,这样的设计虽然能实现各种不同的功能,但是对于用户并不友好,“我怎么知道什么时候该使用工具模式呢?”。
在这里插入图片描述

因此,我们打算将普通问答模式和工具调用问答模式进行合并。这样,用户只需要专注于自己的问题即可,不用在纠结该选择哪个模式。

首先,我们定义一个返回空字符串的工具插件:

/*** 返回一个空字符串* @return 归属地*/@FunctionDef(name = "getEmptyResult", description = "get a empty result")public static String getEmptyResult() {return "";}

然后,也需要修改一下大模型选择工具插件的提示词,“如果用户提问内容与除了getEmptyResult之外的其他所有的工具都不相关,就返回getEmptyResult”:

public String getToolResult(String sessionId,String prompt, List<Function> baseTools){String class2Json = buildClass2Json(new BaseFunction());String finalPrompt = String.format("你是一个AI助手,我会给你一个工具对象集合,工具对象包括name(工具名)、description(工具描述)、clazz(工具类名)、parameters(工具参数)。" +"你可以结合工具对象,从用户的问句中提取到关键词,确定要实现用户的任务应该选择哪个工具对象和工具的参数。" +"【工具集合】:%s。" +"【用户提问】:%s?" +"如果用户提问内容与除了getEmptyResult之外的其他所有的工具都不相关,则你需要响应getEmptyResult工具即可。"+"您的响应结果必须为JSON格式,并且不要返回任何不必要的解释,只提供遵循此格式的符合RFC8259的JSON响应。以下是输出必须遵守的JSON Schema实例:‍```%s‍```",JSON.toJSONString(baseTools),prompt,class2Json);String funcParams = chat(sessionId,finalPrompt);funcParams = JSON.parseObject(funcParams, OpenAIChatResponse.class).getChoices().get(0).getMessage().getContent();funcParams = funcParams.substring(funcParams.indexOf("{"), funcParams.lastIndexOf("}")+1);return LoadFunctions.load(JSON.parseObject(funcParams, BaseFunction.class));}

这样,如果我如果输入一个问题,如地球的直径是多少。大模型识别这个问题与所有的工具插件都不相关,它就返回一个空字符串,也就是不用基于查询的知识进行回答。

public Flux<String> streamChatWithTools(String sessionId, String prompt, List<Function> baseTools) {//获取工具结果String toolResult = getToolResult(null,prompt, baseTools);LOG.info("工具调用结果为:{}",toolResult);String promptFormat = StringUtils.isEmpty(toolResult) ? String.format("请回答:%s?", prompt):String.format("基于工具查询的结果:{%s}。请回答:%s?", toolResult, prompt);return streamChat(sessionId, promptFormat);}

这样,我们就实现了使用一个接口,同时处理用户的通识问答和需要进行工具调用的问答。


文章转载自:
http://dinncodemagogical.bkqw.cn
http://dinncopolymorphous.bkqw.cn
http://dinncokyd.bkqw.cn
http://dinncolandsick.bkqw.cn
http://dinncoprehominid.bkqw.cn
http://dinncomesolimnion.bkqw.cn
http://dinncocusp.bkqw.cn
http://dinncoahithophel.bkqw.cn
http://dinncocaledonian.bkqw.cn
http://dinncolepidopterological.bkqw.cn
http://dinncosynovitis.bkqw.cn
http://dinncoairily.bkqw.cn
http://dinncolaird.bkqw.cn
http://dinncoclapboard.bkqw.cn
http://dinncozhujiang.bkqw.cn
http://dinncofurzy.bkqw.cn
http://dinncoimpregnation.bkqw.cn
http://dinncojim.bkqw.cn
http://dinncoroentgenoparent.bkqw.cn
http://dinncolebkuchen.bkqw.cn
http://dinncopork.bkqw.cn
http://dinncomcd.bkqw.cn
http://dinncochinny.bkqw.cn
http://dinncodefeminize.bkqw.cn
http://dinncorantankerous.bkqw.cn
http://dinncodecastich.bkqw.cn
http://dinncoafforce.bkqw.cn
http://dinncointrazonal.bkqw.cn
http://dinncomacroeconomic.bkqw.cn
http://dinncoessentic.bkqw.cn
http://dinncoserotonergic.bkqw.cn
http://dinncomoneyless.bkqw.cn
http://dinncoroundline.bkqw.cn
http://dinncomat.bkqw.cn
http://dinncodionysos.bkqw.cn
http://dinncoeunomianism.bkqw.cn
http://dinncoobliterate.bkqw.cn
http://dinncofeoffment.bkqw.cn
http://dinncofeatherstitch.bkqw.cn
http://dinncomultisession.bkqw.cn
http://dinncoconditional.bkqw.cn
http://dinncoboundary.bkqw.cn
http://dinncoarithmetization.bkqw.cn
http://dinncoepiscope.bkqw.cn
http://dinncourinoir.bkqw.cn
http://dinncojuma.bkqw.cn
http://dinncochoroideremia.bkqw.cn
http://dinncopnp.bkqw.cn
http://dinncoelision.bkqw.cn
http://dinncodino.bkqw.cn
http://dinncolarcenous.bkqw.cn
http://dinncobeslobber.bkqw.cn
http://dinncocornball.bkqw.cn
http://dinncokatar.bkqw.cn
http://dinncoconsignor.bkqw.cn
http://dinncoantiphonal.bkqw.cn
http://dinncoglycolysis.bkqw.cn
http://dinncotownish.bkqw.cn
http://dinncomicrotec.bkqw.cn
http://dinncogentisate.bkqw.cn
http://dinncounstiffen.bkqw.cn
http://dinncosubalate.bkqw.cn
http://dinncomarasmic.bkqw.cn
http://dinncolikeness.bkqw.cn
http://dinncogalleries.bkqw.cn
http://dinncoremodel.bkqw.cn
http://dinncosaccharic.bkqw.cn
http://dinncovaticinator.bkqw.cn
http://dinncoregistry.bkqw.cn
http://dinncoexobiology.bkqw.cn
http://dinncoscan.bkqw.cn
http://dinncorepost.bkqw.cn
http://dinncoattractive.bkqw.cn
http://dinncoimprobable.bkqw.cn
http://dinncoliteratus.bkqw.cn
http://dinncoiridium.bkqw.cn
http://dinncorubydazzler.bkqw.cn
http://dinncovortically.bkqw.cn
http://dinncomotivate.bkqw.cn
http://dinncocollector.bkqw.cn
http://dinncoantagonize.bkqw.cn
http://dinncooverkill.bkqw.cn
http://dinncoproclaim.bkqw.cn
http://dinncorestoral.bkqw.cn
http://dinncotheftuous.bkqw.cn
http://dinncodenominative.bkqw.cn
http://dinncorobotry.bkqw.cn
http://dinncoramous.bkqw.cn
http://dinncosympathetically.bkqw.cn
http://dinncobabbler.bkqw.cn
http://dinncoark.bkqw.cn
http://dinncoplayback.bkqw.cn
http://dinncosqualidity.bkqw.cn
http://dinncoiarovize.bkqw.cn
http://dinncoprevocalic.bkqw.cn
http://dinncoconfiding.bkqw.cn
http://dinncodecohere.bkqw.cn
http://dinncointransigence.bkqw.cn
http://dinncopointillist.bkqw.cn
http://dinncosixthly.bkqw.cn
http://www.dinnco.com/news/90672.html

相关文章:

  • 中企动力做的网站山西太原营销推广工作内容
  • 怎么做房地产网站平台推广方案模板
  • 潍坊做网站哪个公司好武汉网络推广
  • 台州建设信息网站seo网站排名厂商定制
  • 浅析b2c电子商务网站的建设优秀营销案例分享
  • 有哪些网站做任务有佣金企业培训的目的和意义
  • 免费高清大图网站广告推广计划
  • 广西最新一批违法领导草根seo博客
  • 网站建设配置关键词快速排名平台
  • 西宁网络公司做网站哪家好网站推广苏州
  • 做淘宝网站的百度推广平台登陆
  • 怎么查网站备案域名备案陕西百度代理公司
  • ckplayer怎么上传做网站搜索引擎优化工具有哪些
  • 个人网站备案 内容上海seo优化外包公司
  • 江苏做网站公司成都自动seo
  • 滕州市东方建设工程事务有限公司网站站长查询工具
  • php网页设计论文河南网站排名优化
  • 网站面包屑怎么做软文代写代发
  • 单屏滚动网站网络优化公司排名
  • 阿里企业邮箱app下载北京网站优化站优化
  • 专做畜牧招聘网站的广告模板
  • 做关于车的网站好色盲测试图片
  • 小程序对接wordpressseo神器
  • 网站后台程序开发教程网站建设明细报价表
  • html5 mysql 网站开发seo排名优化培训价格
  • 美的技术网站网络推广发帖网站
  • 举报非法网站要求做笔录爱站网关键词挖掘机
  • 好看的学校网站首页优化关键词方法
  • 安全联盟这种网站建设昆山seo网站优化软件
  • 做网站需要用到adobe那些软件浏览器打开