少有人走的路

勇哥的工业自动化技术网站

让模型能进行联网搜索功能

勇哥注:

要让模型拥有联网搜索功能,仅需要在dashscope.Generation.call中增加一个开关enable_search。

当然,这是在dashscope风格的时候如此,如果是openai风格就不是这样了。


源码:

import os
import dashscope

dashscope.api_key=os.environ.get("DASHSCOPE_API_KEY")

#封装模型响应函数
def call_model(messages, enable_search=False):
    try:
        completion = dashscope.Generation.call(
            model="qwen-plus",
            messages=messages,
            result_format="message",
            stream=False,  # 为了简单起见,先关闭流式输出
            enable_search=enable_search  # 将enable_search作为API参数传入
        )
        return completion
    except Exception as e:
        print(f"调用模型时出错: {e}")
        return None

messages=[
    {"role":"system","content":"你是一个互联搜索助手"},
    {"role":"user","content":"昨天的A股三大指数是多少点"},
]

response=call_model(messages, enable_search=True)  # 开启搜索功能
print(response.output.choices[0].message.content)

题外话:

注意下面的代码,

response=call_model(messages, enable_search=True)

response=call_model(messages, True)

两种都是可以的,前者称为命名参数,是一种更值得推荐的方式。


勇哥搜索了一下C#是否支持命名参数,结果在C#4.0就引入了,只是我不知道罢了。

C#中的命名参数,语法是用:分隔而不是=,而且它还支持乱序调用。

如下:

// 定义方法
void PrintOrderDetails(string sellerName, int orderNum, string productName)
{
    // 方法实现
}

// 使用命名参数调用
PrintOrderDetails(sellerName: "Gift Shop", orderNum: 31, productName: "笔记本");

// 改变参数顺序
PrintOrderDetails(productName: "笔记本", sellerName: "Gift Shop", orderNum: 31);



LLM系列文章:

---------------------------------------------------

deepseek的dashscope调用示例

https://www.skcircle.com/?id=2745

天气查询小例子:测试LLM调用的function功能

https://www.skcircle.com/?id=2746

千问多模态模型qwen-vl-plus:图片中的表格内容提取

https://www.skcircle.com/?id=2747

让模型能进行联网搜索功能

https://www.skcircle.com/?id=2748


发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

«    2025年12月    »
1234567
891011121314
15161718192021
22232425262728
293031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
文章归档
网站收藏
友情链接

Powered By Z-BlogPHP 1.7.3

Copyright www.skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864