This commit is contained in:
fuzhongyun 2025-11-21 10:32:12 +08:00
parent 10cd48667c
commit ee481a8b05
9 changed files with 226 additions and 1 deletions

View File

@ -18,10 +18,15 @@ CHAT_MODEL_BASE_URL=http://localhost:11434
CHAT_MODEL=deepseek-v3.1:671b-cloud CHAT_MODEL=deepseek-v3.1:671b-cloud
# LangSmith监控配置 # LangSmith监控配置
LANGCHAIN_TRACING_V2=true LANGCHAIN_TRACING_V2=true
LANGCHAIN_API_KEY=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 LANGCHAIN_API_KEY=lsv2_pt_e6a2f0ca51874efcacfa0799146d698b_f09cd87b
LANGCHAIN_PROJECT=default LANGCHAIN_PROJECT=default
# LANGSMITH_TRACING=true
# LANGSMITH_API_KEY="<your-langsmith-api-key>"
# LANGSMITH_WORKSPACE_ID="<your-workspace-id>"
# 业务系统API配置 # 业务系统API配置
ORDER_API_BASE_URL=https://api.example.com ORDER_API_BASE_URL=https://api.example.com

View File

@ -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 聊天错误:

174
课件资料/代码.md Normal file
View File

@ -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)
}
```