这里写自定义目录标题
开发前环境准备
安装conda虚拟环境
conda create --name langchain_qwen python=3.9
当出现以下信息时候即为安装成功
安装jupyter 这里忽略,可以网上找教程
安装 ipykernel
pip install ipykernel
把conda虚拟环境到到内核里
activate langchain_qwen
python -m ipykernel install --user --name=langchain_qwen --display-name langchain_qwen
返回:
即为添加成功
退出虚拟环境,启动jupyter
jupyter notebook
进去代码界面后:
可以选择虚拟环境了
第一个通义千文大模型程序
先安装langchain
pip install langchain #安装langchain环境
pip install langchain-community #安装第三方集成,就是各种大语言模型
pip install dashscope #灵积模型服务
申请阿里云 DashScope模型服务灵积的ApiKey
指南:https://help.aliyun.com/zh/dashscope/developer-reference/acquisition-and-configuration-of-api-key?spm=a2c4g.2399481.0.0
步骤:
搜索灵积模型服务 – > 开通服务 -->创建新的API-KEY
编写第一个程序
import os
os.environ["DASHSCOPE_API_KEY"] = 'sk-**'
from langchain_community.chat_models.tongyi import ChatTongyi
from langchain_core.messages import HumanMessage,SystemMessage
chatLLM = ChatTongyi( )
message = [
SystemMessage(
content=
"把英语翻译成中文。"),
HumanMessage(content=
"I love programming."),
]
res = chatLLM(message )
print("chat resp:",res)
执行结果:
chat resp: content='我喜欢编程。' response_metadata={'model_name': 'qwen-turbo', 'finish_reason': 'stop', 'request_id': 'dcf3ee81-6d5a-9831-8977-0132f3953afa', 'token_usage': {'input_tokens': 23, 'output_tokens': 3, 'total_tokens': 26}} id='run-b8ca7639-beb1-4c70-ac19-4040249d84e8-0'
从结果可以看到调用模型成功,并返回正确的翻译语句。可以到阿里云后台看看调用token数量