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

longchain使用通义千问

1.安装第三方库

确保你的python版本大于3.8.1
不然会有以下报错

ERROR: Ignored the following versions that require a different python version: 0.0.1 Requires-Python >=3.8.1,<4.0; 0.0.10 Requires-Python >=3.8.1,<4.0; 0.0.11 Requires-Python >=3.8.1,<4.0; 0.0.12 Requires-Python >=3.8.1,<4.0; 0.0.13 Requires-Python >=3.8.1,<4.0; 0.0.14 Requires-Python >=3.8.1,<4.0; 0.0.15 Requires-Python >=3.8.1,<4.0; 
ERROR: Could not find a version that satisfies the requirement langchain_community (from versions: none)
ERROR: No matching distribution found for langchain_community

我使用了3.10版本的python,按照第三方库:

python -m pip install longchain  langchain-community dashscope  openai==1.7.0 -i https://mirrors.aliyun.com/pypi/simple

2.直接调用通义千问

from openai import OpenAIclient = OpenAI(api_key="", # 如何获取API Key:https://help.aliyun.com/zh/model-studio/developer-reference/get-api-keybase_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)completion = client.chat.completions.create(model="qwen-plus", # 模型列表:https://help.aliyun.com/zh/model-studio/getting-started/modelsmessages=[{'role': 'system', 'content': 'You are a helpful assistant.'},{'role': 'user', 'content': '你是谁?'}]
)
print(completion.choices[0].message.content)

3.使用langchain调用通义千问

from openai import OpenAI
from langchain_community.llms.tongyi import Tongyi
from langchain.prompts.prompt import PromptTemplate
# 设置 API Key(推荐使用环境变量或文件)
import os
os.environ['DASHSCOPE_API_KEY'] = ''def translate():model = Tongyi(model_name='qwen-max',model_kwargs={'temperature': 0.01})prompt_template = PromptTemplate.from_template('你是英语翻译官,对用户的输入翻译成英文,不要解释。\n\n{input}')user_input = input('请输入要翻译的中文:')print('1.用户输入的内容是:', user_input)prompt = prompt_template.format(input=user_input)print(f'2.生成翻译英文的prompts:{prompt}')print(f'3.调用大模型')res = model.invoke(prompt)print(f"4.输出翻译后的英文内容:", res)
translate()

相关文章:

  • 如何对只能有一个`public`顶层类这句话的理解
  • 大文件分片上传进阶版(新增md5校验、上传进度展示、并行控制,智能分片、加密上传、断点续传、自动重试),实现四位一体的网络感知型大文件传输系统‌
  • Maxscript调用Newtonsoft.Json解析Json
  • 从 TinyZero 到 APR:语言模型推理能力的探索与自适应并行化
  • Linux——入门常用基础指令
  • 基于unsloth微调一个越狱大模型
  • Linux——信号(2)信号保存与捕捉
  • CompletableFuture并行处理任务
  • 《MySQL:MySQL表的基本查询操作CRUD》
  • ros2 humble moveit调试笔记
  • docker基本命令1
  • Day-1 漏洞攻击实战
  • QT:Qt5 串口模块 (QSerialPort) 在 VS2015 中正确关闭串口避免被占用
  • 推荐系统/业务,相关知识/概念1
  • Sentinel源码—7.参数限流和注解的实现一
  • 如何在白平衡标定种构建不同类型的白平衡色温坐标系
  • 基于语义网络表示的不确定性推理
  • 从 0 到 1 转型 AI:突破技术壁垒的 5 大核心策略与实战路径
  • RK3588上编译opencv 及基于c++实现图像的读入
  • Java写数据结构:栈
  • 耶路撒冷发生山火,以防长宣布紧急状态
  • 万达电影去年净利润亏损约9.4亿元,计划未来三年内新增25块IMAX银幕
  • 澎湃回声丨23岁小伙“被精神病”8年续:今日将被移出“重精”管理系统
  • 李开复出任福耀科技大学理事会理事,助力学校AI战略
  • 烟花秀、新航线、购物节......上海邮轮文化旅游节今日开幕
  • 解放日报:这是一场需要定力和实力的“科技长征”