项目地址

本系列教程源码Github:https://github.com/zhangdapeng520/zdppy_langchainx

Langchain Github:https://github.com/langchain-ai/langchain

Langchain官方文档:https://python.langchain.com/v0.1/docs/get_started/quickstart/

环境搭建

创建虚拟环境

创建虚拟环境:

conda create --name zdppy_langchainx python==3.11.7

激活虚拟环境:

conda activate zdppy_langchainx

安装Langchain

使用conda安装:

conda install langchain -c conda-forge

Docker搭建Ollama环境

CPU模式

docker run -d -v  -p 11434:11434 --name ollama ollama/ollama

GPU模式

docker run --gpus all -d  -p 11434:11434 --name ollama ollama/ollama

下载并运行AI模型

docker exec -it ollama ollama run qwen:4b-chat

也可以让其在后台运行:

docker exec -itd ollama ollama run qwen:4b-chat
docker logs -f --tail 100 ollama

测试接口的可用性:

curl http://localhost:11434/api/chat -d '{
  "model": "qwen:4b-chat",
  "messages": [
    { "role": "user", "content": "Python是世界上最好的语言吗?" }
  ]
}

编写入门案例

Github上的文件地址如下:

Github: https://github.com/zhangdapeng520/zdppy_langchainx
file: study/official_doc/c01_hello_ollama.py

示例代码:

import os
from langchain_community.llms import Ollama

# 假设你有一个运行在特定IP地址上的模型服务
MODEL_SERVICE_IP = "192.168.77.129"
MODEL_SERVICE_PORT = "11434"

# 设置环境变量,指定代理服务器的IP地址和端口
os.environ['http_proxy'] = f"http://{MODEL_SERVICE_IP}:{MODEL_SERVICE_PORT}"
os.environ['https_proxy'] = f"http://{MODEL_SERVICE_IP}:{MODEL_SERVICE_PORT}"

llm = Ollama(model="qwen:4b-chat")
response = llm.invoke("how can langsmith help with testing?")
print(response)

执行:

python c01_hello_ollama.py

输出如下:

Langsmith is a language generation tool that can be used to create test scenarios.
Here are some ways Langsmith can be useful for creating test scenarios:

  1. Creating realistic user journeys: Langsmith allows you to generate natural-sounding text, which can be used to create realistic user journeys.
  2. Identifying potential issues: Once you have created a realistic user journey using Langsmith, you can use the generated text to identify potential issues in your application or system.
  3. Writing test scenarios: Using the identified potential issues and the natural-sounding text generated by Langsmith, you can write test scenarios that cover all the potential issues identified through Langsmith.
  4. Generating test reports: Once you have written test scenarios using Langsmith, you can use Langsmith to generate test reports that summarize the results of each test scenario.

将问题换成中文的试试:

response = llm.invoke("langsimith能帮我测试langchain编写的代码吗?")
print(response)

得到的答案也是中文的:

langsimith是一个用于自动比较两个文本的开源库。它可以用于自动生成相似度报告,也可以用于文本分类、情感分析等任务。

至于langchain编写的代码,langsimith不能直接帮助你测试langchain编写的代码。但是,你可以使用langsimith来提取和对比langchain编写的代码中的关键信息,从而更好地测试和理解langchain编写的代码。
Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐