From ee481a8b05db8a43ebfe31ab2318890221293b0b Mon Sep 17 00:00:00 2001 From: fuzhongyun <15339891972@163.com> Date: Fri, 21 Nov 2025 10:32:12 +0800 Subject: [PATCH] add --- langchain-project/.env | 7 +- .../logs/langchain_bot_20251119.log | 46 +++++ .../simple_chat_chain.cpython-312.pyc | Bin 15385 -> 15385 bytes .../langsmith_service.cpython-312.pyc | Bin 10049 -> 10049 bytes .../ollama_service.cpython-312.pyc | Bin 13776 -> 13776 bytes .../utils/__pycache__/config.cpython-312.pyc | Bin 5657 -> 5657 bytes .../__pycache__/natural_chat.cpython-312.pyc | Bin 8221 -> 8221 bytes .../order_diagnosis.cpython-312.pyc | Bin 10797 -> 10797 bytes 课件资料/代码.md | 174 ++++++++++++++++++ 9 files changed, 226 insertions(+), 1 deletion(-) create mode 100644 langchain-project/logs/langchain_bot_20251119.log create mode 100644 课件资料/代码.md diff --git a/langchain-project/.env b/langchain-project/.env index 9ee9f9c..5800c72 100644 --- a/langchain-project/.env +++ b/langchain-project/.env @@ -18,10 +18,15 @@ CHAT_MODEL_BASE_URL=http://localhost:11434 CHAT_MODEL=deepseek-v3.1:671b-cloud # LangSmith监控配置 + LANGCHAIN_TRACING_V2=true -LANGCHAIN_API_KEY=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 +LANGCHAIN_API_KEY=lsv2_pt_e6a2f0ca51874efcacfa0799146d698b_f09cd87b LANGCHAIN_PROJECT=default +# LANGSMITH_TRACING=true +# LANGSMITH_API_KEY="" +# LANGSMITH_WORKSPACE_ID="" + # 业务系统API配置 ORDER_API_BASE_URL=https://api.example.com diff --git a/langchain-project/logs/langchain_bot_20251119.log b/langchain-project/logs/langchain_bot_20251119.log new file mode 100644 index 0000000..3a9800e --- /dev/null +++ b/langchain-project/logs/langchain_bot_20251119.log @@ -0,0 +1,46 @@ +2025-11-19 11:25:43,697 | INFO | langsmith_service | info:107 | ✅ LangSmith 监控已启用 +2025-11-19 11:25:43,818 | INFO | ollama_service | info:107 | ✅ Ollama模型初始化成功 +2025-11-19 11:25:43,819 | INFO | ollama_service | info:107 | 路由模型: qwen3:8b +2025-11-19 11:25:43,819 | INFO | ollama_service | info:107 | 对话模型: deepseek-v3.1:671b-cloud +2025-11-19 11:25:44,701 | INFO | workflow | info:107 | 🔗 SimpleChatChain 初始化完成 +2025-11-19 11:25:44,708 | INFO | workflow | info:107 | 📊 工作流图构建完成 +2025-11-19 11:25:44,708 | INFO | workflow | info:107 | 🚀 ChatWorkflow 初始化完成 +2025-11-19 11:25:44,708 | INFO | workflow | info:107 | 🤖 使用 LangGraph 工作流模式 +2025-11-19 11:26:35,020 | INFO | workflow | info:107 | 🔗 SimpleChatChain 初始化完成 +2025-11-19 11:26:42,252 | INFO | workflow | info:107 | 🌊 开始流式对话: 2c99f8ef-48a9-4697-999f-c7d6e24d961a +2025-11-19 11:26:42,253 | ERROR | workflow | error:115 | ❌ 流式对话失败: LangSmithService.log_model_call() got an unexpected keyword argument 'input_messages' +2025-11-19 11:27:00,025 | ERROR | workflow | error:115 | CLI 聊天错误: +2025-11-19 14:21:56,343 | INFO | langsmith_service | info:107 | ✅ LangSmith 监控已启用 +2025-11-19 14:21:56,452 | INFO | ollama_service | info:107 | ✅ Ollama模型初始化成功 +2025-11-19 14:21:56,452 | INFO | ollama_service | info:107 | 路由模型: qwen3:8b +2025-11-19 14:21:56,452 | INFO | ollama_service | info:107 | 对话模型: deepseek-v3.1:671b-cloud +2025-11-19 14:21:56,948 | INFO | workflow | info:107 | 🔗 SimpleChatChain 初始化完成 +2025-11-19 14:21:56,954 | INFO | workflow | info:107 | 📊 工作流图构建完成 +2025-11-19 14:21:56,955 | INFO | workflow | info:107 | 🚀 ChatWorkflow 初始化完成 +2025-11-19 14:21:56,955 | INFO | workflow | info:107 | 🤖 使用 LangGraph 工作流模式 +2025-11-19 14:22:13,892 | INFO | workflow | info:107 | 🚀 工作流开始: 788a217a-b62a-4d85-9ef8-067dbdc8a4d5 +2025-11-19 14:22:13,893 | INFO | workflow | info:107 | 🚀 节点开始: intent_analysis +2025-11-19 14:22:13,893 | INFO | workflow | info:107 | 🚀 节点开始: intent_analysis +2025-11-19 14:22:17,743 | INFO | workflow | info:107 | 🎯 意图分析: 'helper' -> general_chat (置信度: 0.85) +2025-11-19 14:22:17,743 | INFO | workflow | info:107 | ✅ 节点完成: intent_analysis +2025-11-19 14:22:17,743 | INFO | workflow | info:107 | 💬 路由到自然对话 (意图: IntentType.GENERAL_CHAT, 置信度: 0.85) +2025-11-19 14:22:17,743 | INFO | workflow | info:107 | 🚀 节点开始: natural_chat +2025-11-19 14:22:17,744 | INFO | workflow | info:107 | 💬 构建对话消息: 2 条消息 +2025-11-19 14:22:17,744 | INFO | workflow | info:107 | 🚀 节点开始: chat_generation +2025-11-19 14:22:20,087 | INFO | workflow | info:107 | ✅ 节点完成: natural_chat +2025-11-19 14:22:20,087 | INFO | workflow | info:107 | ✅ 工作流完成: 788a217a-b62a-4d85-9ef8-067dbdc8a4d5 +2025-11-19 14:24:25,745 | INFO | workflow | info:107 | 🚀 工作流开始: 788a217a-b62a-4d85-9ef8-067dbdc8a4d5 +2025-11-19 14:24:25,746 | INFO | workflow | info:107 | 🚀 节点开始: intent_analysis +2025-11-19 14:24:25,747 | INFO | workflow | info:107 | 🚀 节点开始: intent_analysis +2025-11-19 14:24:27,889 | INFO | workflow | info:107 | 🎯 意图分析: '订单 564875' -> order_inquiry (置信度: 0.95) +2025-11-19 14:24:27,921 | INFO | workflow | info:107 | ✅ 节点完成: intent_analysis +2025-11-19 14:24:27,921 | INFO | workflow | info:107 | 🎯 路由到订单诊断 (置信度: 0.95) +2025-11-19 14:24:27,921 | INFO | workflow | info:107 | 🚀 节点开始: order_diagnosis +2025-11-19 14:24:28,429 | INFO | workflow | info:107 | 📦 获取订单信息: 564875 +2025-11-19 14:24:28,933 | INFO | workflow | info:107 | 🚀 节点开始: diagnosis_thinking +2025-11-19 14:24:42,417 | INFO | workflow | info:107 | 🔍 生成诊断结果: 564875 +2025-11-19 14:24:42,418 | INFO | workflow | info:107 | 🚀 节点开始: chat_generation +2025-11-19 14:24:48,316 | INFO | workflow | info:107 | 💬 生成最终回复: 564875 +2025-11-19 14:24:48,319 | INFO | workflow | info:107 | ✅ 节点完成: order_diagnosis +2025-11-19 14:24:48,319 | INFO | workflow | info:107 | ✅ 工作流完成: 788a217a-b62a-4d85-9ef8-067dbdc8a4d5 +2025-11-19 14:30:27,321 | ERROR | workflow | error:115 | CLI 聊天错误: diff --git a/langchain-project/src/chains/__pycache__/simple_chat_chain.cpython-312.pyc b/langchain-project/src/chains/__pycache__/simple_chat_chain.cpython-312.pyc index 2d95cedc03b4fac5570dd4b9f5f6ec0991bb606c..3423e07116f90f8e8b1a8a8ea65b72ee637a94e8 100644 GIT binary patch delta 20 acmbPPF|&gEG%qg~0}#CZ_j4mRpA7&?p9cp3 delta 20 acmbPPF|&gEG%qg~0}%8~|FDsp&jtWRVFoq; diff --git a/langchain-project/src/services/__pycache__/langsmith_service.cpython-312.pyc b/langchain-project/src/services/__pycache__/langsmith_service.cpython-312.pyc index 712b20a0003fca664ce3d1553568f4f06df73db3..f75d8ab36d10c44e9d13d70628e336a5dace2fbf 100644 GIT binary patch delta 21 bcmX@;chHaLG%qg~0}z-e%4TllF;xcuL)!&W delta 21 bcmX@;chHaLG%qg~0}${``;f7b$5b5vNaF@A diff --git a/langchain-project/src/services/__pycache__/ollama_service.cpython-312.pyc b/langchain-project/src/services/__pycache__/ollama_service.cpython-312.pyc index 22a0b97b9a6c6df27a3660c833699759ccfac039..ec47663c134176f6242c12da84460844e3185702 100644 GIT binary patch delta 21 bcmcbReIc9YG%qg~0}xEIkG%qg~0}$9J%4Tll;S~b_IBo=t delta 21 bcmbQKGgF7>G%qg~0}y=b`;f7bhgS>$L@owC diff --git a/langchain-project/src/workflows/nodes/__pycache__/natural_chat.cpython-312.pyc b/langchain-project/src/workflows/nodes/__pycache__/natural_chat.cpython-312.pyc index a378a7973c5540ff94fee93bcbe9e9354eeaeae1..75cde5e4e003ab5c09b54ff15c591af8b9f99c04 100644 GIT binary patch delta 20 acmbR1FxP?mG%qg~0}#CZ_j4n+kOBZhLk5Qc delta 20 acmbR1FxP?mG%qg~0}%L6|FDr;NC5ymS_OFk diff --git a/langchain-project/src/workflows/nodes/__pycache__/order_diagnosis.cpython-312.pyc b/langchain-project/src/workflows/nodes/__pycache__/order_diagnosis.cpython-312.pyc index 642098ed318a9cbf97dd39403e3e264850700c6b..167e4733e165060da091eaf2567deb2582a6008d 100644 GIT binary patch delta 20 acmZ1*vNnYKG%qg~0}#CZ_j4n+k`@3+od)Ru delta 20 acmZ1*vNnYKG%qg~0}vEU|FDr;Neci%n+5y; diff --git a/课件资料/代码.md b/课件资料/代码.md new file mode 100644 index 0000000..4f91d94 --- /dev/null +++ b/课件资料/代码.md @@ -0,0 +1,174 @@ +# 代码示例 + +langchain生态 + +- Chain + +```python +async def main(): + prompt = ChatPromptTemplate.from_messages([ + ("system", "你是一个友好、专业的AI助手。"), + ("human", "{input}") + ]) + + # 2) 定义模型(按你本地 Ollama 配置调整) + model = ChatOllama( + model="qwen3:8b", # 或你的实际模型名 + base_url="http://localhost:11434" + ) + + # 3) 定义输出解析器,将 AIMessage 转字符串 + parser = StrOutputParser() + + # 4) 组合为表达式管道 + chain = prompt | model | parser + + # 非流式调用 + text = await chain.ainvoke({"input": "请用一句话做自我介绍"}) + print("非流式:", text) + + # 流式调用 + print("流式:", end="", flush=True) + async for chunk in chain.astream({"input": "讲一个简短的笑话"}): + print(chunk, end="", flush=True) +``` + +- Graph + +```python +# 创建状态图 +async def main(): + workflow = StateGraph(WorkflowState) + + # 添加节点 + workflow.add_node("intent_analysis", self._intent_analysis_wrapper) + workflow.add_node("order_diagnosis", self._order_diagnosis_wrapper) + workflow.add_node("natural_chat", self._natural_chat_wrapper) + + # 设置入口点 + workflow.set_entry_point("intent_analysis") + + # 添加条件边 + workflow.add_conditional_edges( + "intent_analysis", + self._route_after_intent_analysis, + { + "order_diagnosis": "order_diagnosis", + "natural_chat": "natural_chat", + "end": END + } + ) + + # 添加结束边 + workflow.add_edge("order_diagnosis", END) + workflow.add_edge("natural_chat", END) + + # 编译图 + agent = workflow.compile() + + messages = [HumanMessage(content="Add 3 and 4.")] + messages = agent.invoke({"messages": messages}) + for m in messages["messages"]: + print(m.content) +``` + +对应流程图(Mermaid) + +```mermaid +flowchart TD + IA[intent_analysis] --> ROUTE{判断意图} + ROUTE -->|order_diagnosis| OD[order_diagnosis] + ROUTE -->|natural_chat| NC[natural_chat] + ROUTE -->|end| END((END)) + + OD --> END + NC --> END +``` + +说明:当意图分析结果为空或不满足条件时,视为“否”分支,进入 `natural_chat`。 + +- Agent + +```python +from langchain.agents import create_agent + +def get_weather(city: str) -> str: + """Get weather for a given city.""" + return f"It's always sunny in {city}!" + +agent = create_agent( + model="claude-sonnet-4-5-20250929", + tools=[get_weather], + system_prompt="You are a helpful assistant", +) + +# Run the agent +agent.invoke( + {"messages": [{"role": "user", "content": "what is the weather in sf"}]} +) +``` + +Go生态 + +- chain + +```go +package main + +import ( + "context" + "fmt" + "os" + + "github.com/cloudwego/eino/compose" + "github.com/cloudwego/eino/components/prompt" + "github.com/cloudwego/eino/schema" + "github.com/cloudwego/eino-ext/components/model/openai" +) + +func main() { + ctx := context.Background() + + // 1) 定义 ChatTemplate(输入键:query),使用 Jinja2 格式 + pt := prompt.FromMessages( + schema.Jinja2, + schema.SystemMessage("你是一个友好的AI助手。请用中文回答。"), + schema.UserMessage("用户问题:{{query}}"), + ) + + // 2) 创建 ChatModel(以 OpenAI 为例,按需替换为其他提供商) + cm, err := openai.NewChatModel(ctx, &openai.ChatModelConfig{ + APIKey: os.Getenv("OPENAI_API_KEY"), + BaseURL: os.Getenv("OPENAI_BASE_URL"), // 可选,自建或代理时设置 + Model: os.Getenv("MODEL_NAME"), // 例如:"gpt-4o-mini" + }) + if err != nil { + fmt.Println("初始化模型失败:", err) + return + } + + // 3) 组合并编译 Chain:模板 -> 模型 + chain, err := compose.NewChain[map[string]any, *schema.Message](). + AppendChatTemplate(pt). // prompt + AppendChatModel(cm). // model + // AppendBranch 分支 + // AppendPassthrough 空透传节点 + // AppendParallel 并行节点 + // AppendGraph 子图节点 + // AppendLambda 自定义函数节点 + Compile(ctx) // 编译链 + if err != nil { + fmt.Println("编译链失败:", err) + return + } + + // 4) 调用链(非流式) + msg, err := chain.Invoke(ctx, map[string]any{"query": "简要介绍一下 Eino 的 Chain"}) + if err != nil { + fmt.Println("调用失败:", err) + return + } + + fmt.Println("回复:", msg.Content) +} +```