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

烟台市城市建设发展有限公司网站要建网站

烟台市城市建设发展有限公司网站,要建网站,名片设计图片,工程建设教育培训在实际应用过程中,我们经常会部署开源的大模型并将其定义成服务接口,或者做成web页面的形式,这样就能使用和测试大模型的推理能力。我们在使用kimi,千问,deepseek时会发现他们都有一个联网功能,大模型增加了联网搜索功…

在实际应用过程中,我们经常会部署开源的大模型并将其定义成服务接口,或者做成web页面的形式,这样就能使用和测试大模型的推理能力。我们在使用kimi,千问,deepseek时会发现他们都有一个联网功能,大模型增加了联网搜索功能后,能够实时为大模型提供推理所需的素材,减少大模型的幻觉,提供大模型的推理准确性。

离线的大模型可以通过搜索接口实现联网搜索功能,常用的搜索引擎几乎都会提供API接口,其中倍受推荐的是tavily,但这些API接口都是收费的,本文使用DuckDuckGo来实现离线大模型的联网搜索功能,DuckDuckGo的好处就是免费。

1.启动虚拟环境

根据自己的实际情况启动运行大模型的虚拟环境,在本例中我使用了千问的模型,所以在我的测试服务器上我使用以下命令启动了千问的虚拟环境。

conda activate qwen

2.安装依赖

pip install -qU langchain-openai
pip install -qU langchain-anthropic
pip install -qU langchain_community
pip install -qU langchain_experimental
pip install -qU langgraph
pip install -qU duckduckgo-search

📓 如果你使用kaggle的notebook环境,还需要安装accelerateipywidgets

3.代码示例

3.1 大模型不联网的示例

import os
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
from langchain_core.prompts import ChatPromptTemplate# 设置模型标识符或本地模型目录
model_dir = "Qwen/Qwen2-7B-Instruct"# 加载分词器和模型
tokenizer = AutoTokenizer.from_pretrained(model_dir, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(model_dir,return_dict=True,low_cpu_mem_usage=True,torch_dtype=torch.float16,device_map="auto",trust_remote_code=True,
)# 使用 transformers 的 pipeline 初始化聊天模型
chat = pipeline('text-generation', model=model, tokenizer=tokenizer)# 定义聊天提示模板(只包含系统提示和用户输入)
prompt = ChatPromptTemplate.from_messages([("system", "You are a helpful assistant."),("user", "{input}")
])# 创建一个提问和生成回答的链
def chain(input_text):# 格式化提示模板,将用户输入填入prompt_text = prompt.format(input=input_text)# 使用模型生成回复response = chat(prompt_text, max_length=512, num_return_sequences=1)return response[0]['generated_text']# 示例用法
response = chain("青岛今天的天气如何")
print(response)

运行上面的代码后,会出现如下的输出,说明大模型并没有联网功能。
在这里插入图片描述

3.2 大模型联网后的示例

from langchain_community.tools import DuckDuckGoSearchRun
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser# 初始化 DuckDuckGoSearchRun 工具
search_tool = DuckDuckGoSearchRun()# 初始化模型
import os
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline# 设置模型保存路径
model_dir = './Qwen2-7B-Instruct'# 如果模型目录不存在,则下载并保存模型
if not os.path.exists(model_dir):os.makedirs(model_dir)base_model = "Qwen/Qwen2-7B-Instruct"tokenizer = AutoTokenizer.from_pretrained(base_model)tokenizer.save_pretrained(model_dir)model = AutoModelForCausalLM.from_pretrained(base_model,return_dict=True,low_cpu_mem_usage=True,torch_dtype=torch.float16,device_map="auto",trust_remote_code=True,)model.save_pretrained(model_dir)
else:tokenizer = AutoTokenizer.from_pretrained(model_dir)model = AutoModelForCausalLM.from_pretrained(model_dir,return_dict=True,low_cpu_mem_usage=True,torch_dtype=torch.float16,device_map="auto",trust_remote_code=True,)# 初始化聊天模型
chat = pipeline('text-generation', model=model, tokenizer=tokenizer)# 为聊天机器人定义一个提示模板
prompt = ChatPromptTemplate.from_messages([("system", "You are a helpful assistant. Use the search tool to find up-to-date information if needed."),("user", "{input}"),("assistant", "{search_results}"),
])# 创建一个集成搜索工具和 LLM(大型语言模型)的链
def chain(input_text):# 获取搜索结果search_results = search_tool.invoke({'query': input_text})# 将搜索结果和用户输入整合到提示模板中prompt_text = prompt.format(input=input_text, search_results=search_results)# 生成响应response = chat(prompt_text, max_length=512, num_return_sequences=1)return response[0]['generated_text']# 示例用法
response = chain("青岛今天的天气如何")
print(response)

运行上面的代码后,会输出如下图所示的输出,说明模型具备了联网的功能。
在这里插入图片描述
👿 由于duckduckgo需要梯子才能使用,所以上面的示例要有科学上网的环境才行。


文章转载自:

http://ksZ5UbTp.hkfhp.cn
http://6e583Wyp.hkfhp.cn
http://Q7CdV6dP.hkfhp.cn
http://JRRy40MO.hkfhp.cn
http://2UwCf4FA.hkfhp.cn
http://v31JeN9a.hkfhp.cn
http://FvY17wXO.hkfhp.cn
http://RS9wbEEo.hkfhp.cn
http://FafQKl98.hkfhp.cn
http://lNTZtxKA.hkfhp.cn
http://9fKVG6Lq.hkfhp.cn
http://sHGHL2jR.hkfhp.cn
http://sPO4jRCp.hkfhp.cn
http://e63XivgG.hkfhp.cn
http://hZG1mUAa.hkfhp.cn
http://ku6IeGS7.hkfhp.cn
http://BLLIOCMJ.hkfhp.cn
http://JJBlxdba.hkfhp.cn
http://Izfjolqi.hkfhp.cn
http://v2hCZrdi.hkfhp.cn
http://Pn80dvai.hkfhp.cn
http://1YtewKPa.hkfhp.cn
http://AlvKMbQ9.hkfhp.cn
http://HGOf6SyE.hkfhp.cn
http://yJ9oocsZ.hkfhp.cn
http://pfwcXI5s.hkfhp.cn
http://AsHfdmpp.hkfhp.cn
http://wl0augdp.hkfhp.cn
http://qJw2LPYC.hkfhp.cn
http://Mp2V4nKe.hkfhp.cn
http://www.dtcms.com/wzjs/624033.html

相关文章:

  • 网站建设 qq业务网制作网站建设协议需要注意的问题
  • 网站上的菠菜游戏哪里可以做代码给wordpress添加图片
  • 网站设计常用软件都有哪些互联网推广开户
  • wordpress建站云盘服务类型网站开发需要哪些技术
  • 建网站费用 优帮云天津网站建设论坛
  • 学校网站建设考评办法网站制作价格服务
  • 湖南做网站的公司有哪些有网站模板怎么建站
  • 濮阳高端网站建设平面设计有什么网站
  • 网站网页设计怎样园林景观设计公司管理流程
  • wordpress云建站系统wordpress 显示字体大小
  • 凡科建站模板免费网站推广服务
  • 网站程序语言论坛类网站备案吗
  • 某班级网站建设方案论文衡阳市住建局官方网站
  • 建设购物网站流程wordpress my02visitors
  • 国外设计素材网站wordpress音乐页面
  • 六安做网站多少钱个人工作室网页设计模板
  • 医疗网站建设行业现状注册公司费用流程
  • 全媒体网站的建设方案柚子皮wordpress移动主题
  • 做h5页面的网站蘑菇泰安电视台新闻综合频道
  • 网站收录有什么好处WordPress手机显示内容
  • 优化网站结构一般包括经典广告
  • jsp网站开发难点北京的网站建设公司有哪些
  • 广州网站推广运营网站怎么做才能被百度抓取到
  • 百度做网站多少钱能做wordpress 微博】
  • 山东平台网站建设设计莱芜金点子2023最新招聘
  • 有谁有做卫生纸巾的网站重庆网站建设公司 十年
  • 网站开发实施方案wordpress怎么制作菜单
  • 建站推广营销技巧和营销方法心得
  • 网站建设有什么要求天猫网站的建设
  • 企业网站的价值体现是在网页设计中文本居中用什么代码