【工程化实践】VSCode Python全栈配置:LangGraph框架开发指南

一、LangGraph入门指南 - 在VS Code中设置AI代理环境

你好! 你是否看了官方文档,却不知道如何下手开始创建LangGraph项目?Github上项目琳琅满目、五花八门,学习LangGraph还是要打好基础,本文带你配置VSCODE环境,并从0新建一个LangGraph项目!

VSCODE环境配置

项目结构如下:

在这里插入图片描述

  1. 在本地新建文件夹langgraph-chat ,并使用vscode打开该文件夹;

  2. 在vscode Terminal创建虚拟环境:输入命令python -m venv venv;

  3. 激活虚拟环境:输入命令venv\Scripts\activate;
    在这里插入图片描述

  4. 安装langgraph,输入命令pip install langgraph;

  5. 将本地安装的包名保存到txt文件中 :输入命令pip freeze > requirements.txt;

在这里插入图片描述

  1. 创建.env文件,填写OPEN_API_KEY ,因为我用的国内代理,所以要把OPENAI_BASE_URL也写上;

在这里插入图片描述

  1. 安装 python-dotenv , pip install python-dotenv,python-dotenv 是一个用于管理环境变量的 Python 库,它可以让你将配置项(如 API 密钥、数据库连接字符串等)存储在 .env 文件中,并在代码中加载这些变量。这种做法非常有用,尤其是在开发、测试、和生产环境中,不同环境的配置可能不同。具体参考:https://blog.csdn.net/weixin_48321392/article/details/144589737
  2. 安装 pip install langchain_openai ,用于在langchain中使用openai接口。

到这里环境就配置好了,

在这里插入图片描述

二、Agent如何思考 | 使用VSCODE基于LangGraph 框架构建一个能思考的AI Agent

智能体的结构如下图所示,包括两个节点,和三条边,
在这里插入图片描述

在vscode中文件-新建文件,选择Jupyter Notebook,命名为thinking_bot
在这里插入图片描述

项目结构如下:
在这里插入图片描述
代码如下,把vscode中的ipynb文件转换成了md文件贴在这里了:

from dotenv import load_dotenv
load_dotenv()
True
from langchain_openai import ChatOpenAI
import os 

openai_api_key = os.getenv("OPENAI_API_KEY")
openai_api_base = os.getenv("OPENAI_BASE_URL")
llm = ChatOpenAI(
    openai_api_key=openai_api_key,
    openai_api_base=openai_api_base,
    model_name="o1-preview",
)
from typing import TypedDict 


class InputState(TypedDict):
    question : str

class OutputState(TypedDict):
    answer : str

class OverallState(TypedDict):
    question: str
    answer: str
    notes: str

from langgraph.graph import StateGraph,START,END

graph = StateGraph(OverallState, input = InputState, output= OutputState)
from langchain_core.messages import HumanMessage,SystemMessage
system_msg = SystemMessage(
    content='You are helpful assistant which helps how to answer a question'
)

def thinking_node(state: InputState)->OverallState:
    print("Thinking")
    notes = llm.invoke([system_msg] + [HumanMessage(content=state["question"])]).content
    print("***************")
    print(notes)
    print("****************")
    return {"answer": "", "notes": notes, "question": state["question"]}
graph.add_node("thinking_node", thinking_node)
<langgraph.graph.state.StateGraph at 0x228d5303790>
graph.add_edge(START, "thinking_node")
<langgraph.graph.state.StateGraph at 0x228d5303790>
def answer_node(state: OverallState) -> OutputState:
    answer = llm.invoke([HumanMessage(content=state["question"] + state["notes"])]).content
    return {"answer": answer}
graph.add_node("answer_node", answer_node)

<langgraph.graph.state.StateGraph at 0x228d5303790>
graph.add_edge("thinking_node", "answer_node")
graph.add_edge("answer_node", END)
<langgraph.graph.state.StateGraph at 0x228d5303790>
thinking_bot = graph.compile()
thinking_bot
thinking_bot.invoke({"question": "距离中国春节还有几天?"})
Thinking
***************
> Thinking
**Determining the timeframe**
I’m piecing together today's date and looking forward to marking the days until Chinese New Year.
**Figuring out the duration**
Calculating the number of days until Chinese New Year involves identifying today's date and subtracting this from the official New Year date to see how many days remain.
**Subtracting days**
Figuring out the days until Chinese New Year involves subtracting today’s date from the official New Year date, ensuring both are formatted accurately. This straightforward calculation shows progress day by day.
**Pinpointing today's date**
I’m figuring out today’s date to calculate the days until Chinese New Year. Starting with January 11, 2023, I’m counting days from January 11 to February 10.
**Mapping the days**
I’m laying out the days until Chinese New Year by starting with January 11, 2023, and counting forward to February 10. This helps in determining the exact number of days left.
**Pulling together the information**
OK, let me see. By linking the initial date to the Chinese New Year, I count the days incrementally from January 11, 2023 to February 10.
Thought for 4 seconds

中国的春节通常是在农历正月初一,而2025年的春节是在2月10日。所以,距离2025年春节还有大约23天。
****************



{'answer': '> Thinking\nClarifying the date and timeframe calculation steps. Progressing through counting days from January 11 to February 10. Progress is steady in piecing together how long until Chinese New Year.\nI’m verifying the timeline and confirming that Chinese New Year in 2025 falls on January 29. This aligns with the data for previous years and updates the current context.\nPiecing together the accurate date from January 18, 2025, until January 29, 2025, leads to 11 days remaining. There appears to be some confusion regarding the Chinese New Year date in 2025.\nClarifying the Chinese New Year dates for 2025 and 2024 to resolve the timing confusion. Emphasizing the accuracy of January 29, 2025, for CNY.\nClarifying the correct date: January 29, 2025. The user mistakenly referenced February 10, 2024. Progressing towards accurate understanding by addressing the incorrect date.\nThought about Chinese New Year countdown for 12 seconds\n\n根据万年历和权威天文历法信息,**2025年的农历新年(春节)在公历的1月29日**。  \n如果今天是公历 2025 年 1 月 18 日,那么距离 2025 年春节还有:\n\n1. 月份与日期:1 月 29 日 - 1 月 18 日  \n2. 直接相减:29 - 18 = **11 天**  \n\n所以,**距离 2025 年春节还有 11 天**。  \n\n> 有些信息源可能把「2024 年春节」(2 月 10 日) 混淆为 2025 年的日期,实际 2025 年春节确切是在 **1 月 29 日**。'}

执行结果如下:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

alena_citc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值