@zimezone/z-command 1.0.1 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +57 -38
- package/dist/cli.js +14 -2
- package/dist/cli.js.map +1 -1
- package/dist/commands/init.d.ts +1 -7
- package/dist/commands/init.d.ts.map +1 -1
- package/dist/commands/init.js +101 -23
- package/dist/commands/init.js.map +1 -1
- package/dist/commands/update.d.ts +11 -0
- package/dist/commands/update.d.ts.map +1 -0
- package/dist/commands/update.js +88 -0
- package/dist/commands/update.js.map +1 -0
- package/dist/platforms.d.ts +21 -0
- package/dist/platforms.d.ts.map +1 -0
- package/dist/platforms.js +137 -0
- package/dist/platforms.js.map +1 -0
- package/dist/types.d.ts +44 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +6 -0
- package/dist/types.js.map +1 -0
- package/package.json +13 -5
- package/templates/agents/api-documenter.agent.md +161 -0
- package/templates/agents/architect-review.agent.md +146 -0
- package/templates/agents/arm-cortex-expert.agent.md +288 -0
- package/templates/agents/backend-architect.agent.md +309 -0
- package/templates/agents/backend-security-coder.agent.md +152 -0
- package/templates/agents/bash-pro.agent.md +285 -0
- package/templates/agents/c-pro.agent.md +35 -0
- package/templates/agents/c4-code.agent.md +320 -0
- package/templates/agents/c4-component.agent.md +227 -0
- package/templates/agents/c4-container.agent.md +248 -0
- package/templates/agents/c4-context.agent.md +235 -0
- package/templates/agents/conductor-validator.agent.md +245 -0
- package/templates/agents/csharp-pro.agent.md +38 -0
- package/templates/agents/customer-support.agent.md +148 -0
- package/templates/agents/database-admin.agent.md +142 -0
- package/templates/agents/database-architect.agent.md +238 -0
- package/templates/agents/database-optimizer.agent.md +144 -0
- package/templates/agents/debugger.agent.md +30 -0
- package/templates/agents/deployment-engineer.agent.md +0 -0
- package/templates/agents/devops-troubleshooter.agent.md +138 -0
- package/templates/agents/django-pro.agent.md +159 -0
- package/templates/agents/docs-architect.agent.md +77 -0
- package/templates/agents/dotnet-architect.agent.md +175 -0
- package/templates/agents/dx-optimizer.agent.md +63 -0
- package/templates/agents/elixir-pro.agent.md +38 -0
- package/templates/agents/error-detective.agent.md +32 -0
- package/templates/agents/event-sourcing-architect.agent.md +42 -0
- package/templates/agents/fastapi-pro.agent.md +171 -0
- package/templates/agents/firmware-analyst.agent.md +330 -0
- package/templates/agents/frontend-security-coder.agent.md +149 -0
- package/templates/agents/haskell-pro.agent.md +37 -0
- package/templates/agents/hr-pro.agent.md +105 -0
- package/templates/agents/incident-responder.agent.md +190 -0
- package/templates/agents/ios-developer.agent.md +198 -0
- package/templates/agents/java-pro.agent.md +156 -0
- package/templates/agents/javascript-pro.agent.md +35 -0
- package/templates/agents/julia-pro.agent.md +187 -0
- package/templates/agents/legal-advisor.agent.md +49 -0
- package/templates/agents/malware-analyst.agent.md +272 -0
- package/templates/agents/mermaid-expert.agent.md +39 -0
- package/templates/agents/minecraft-bukkit-pro.agent.md +104 -0
- package/templates/agents/mobile-security-coder.agent.md +163 -0
- package/templates/agents/monorepo-architect.agent.md +44 -0
- package/templates/agents/observability-engineer.agent.md +228 -0
- package/templates/agents/performance-engineer.agent.md +167 -0
- package/templates/agents/php-pro.agent.md +43 -0
- package/templates/agents/posix-shell-pro.agent.md +284 -0
- package/templates/agents/quant-analyst.agent.md +32 -0
- package/templates/agents/reference-builder.agent.md +167 -0
- package/templates/agents/reverse-engineer.agent.md +202 -0
- package/templates/agents/risk-manager.agent.md +41 -0
- package/templates/agents/ruby-pro.agent.md +35 -0
- package/templates/agents/rust-pro.agent.md +156 -0
- package/templates/agents/sales-automator.agent.md +35 -0
- package/templates/agents/scala-pro.agent.md +60 -0
- package/templates/agents/search-specialist.agent.md +59 -0
- package/templates/agents/security-auditor.agent.md +138 -0
- package/templates/agents/seo-authority-builder.agent.md +116 -0
- package/templates/agents/seo-cannibalization-detector.agent.md +103 -0
- package/templates/agents/seo-content-auditor.agent.md +63 -0
- package/templates/agents/seo-content-planner.agent.md +88 -0
- package/templates/agents/seo-content-refresher.agent.md +98 -0
- package/templates/agents/seo-content-writer.agent.md +76 -0
- package/templates/agents/seo-keyword-strategist.agent.md +75 -0
- package/templates/agents/seo-meta-optimizer.agent.md +72 -0
- package/templates/agents/seo-snippet-hunter.agent.md +94 -0
- package/templates/agents/seo-structure-architect.agent.md +88 -0
- package/templates/agents/service-mesh-expert.agent.md +41 -0
- package/templates/agents/sql-pro.agent.md +146 -0
- package/templates/agents/tdd-orchestrator.agent.md +183 -0
- package/templates/agents/temporal-python-pro.agent.md +349 -0
- package/templates/agents/terraform-specialist.agent.md +137 -0
- package/templates/agents/test-automator.agent.md +203 -0
- package/templates/agents/threat-modeling-expert.agent.md +44 -0
- package/templates/agents/tutorial-engineer.agent.md +118 -0
- package/templates/agents/ui-ux-designer.agent.md +188 -0
- package/templates/agents/ui-visual-validator.agent.md +192 -0
- package/templates/agents/vector-database-engineer.agent.md +43 -0
- package/templates/skills/angular-migration/SKILL.md +410 -0
- package/templates/skills/api-design-principles/SKILL.md +528 -0
- package/templates/skills/api-design-principles/assets/api-design-checklist.md +155 -0
- package/templates/skills/api-design-principles/assets/rest-api-template.py +182 -0
- package/templates/skills/api-design-principles/references/graphql-schema-design.md +583 -0
- package/templates/skills/api-design-principles/references/rest-best-practices.md +408 -0
- package/templates/skills/architecture-decision-records/SKILL.md +428 -0
- package/templates/skills/architecture-patterns/SKILL.md +494 -0
- package/templates/skills/async-python-patterns/SKILL.md +694 -0
- package/templates/skills/auth-implementation-patterns/SKILL.md +634 -0
- package/templates/skills/changelog-automation/SKILL.md +552 -0
- package/templates/skills/code-review-excellence/SKILL.md +520 -0
- package/templates/skills/competitive-landscape/SKILL.md +479 -0
- package/templates/skills/context-driven-development/SKILL.md +385 -0
- package/templates/skills/cost-optimization/SKILL.md +274 -0
- package/templates/skills/cqrs-implementation/SKILL.md +554 -0
- package/templates/skills/data-quality-frameworks/SKILL.md +587 -0
- package/templates/skills/data-storytelling/SKILL.md +453 -0
- package/templates/skills/database-migration/SKILL.md +424 -0
- package/templates/skills/dbt-transformation-patterns/SKILL.md +561 -0
- package/templates/skills/debugging-strategies/SKILL.md +527 -0
- package/templates/skills/defi-protocol-templates/SKILL.md +454 -0
- package/templates/skills/dependency-upgrade/SKILL.md +409 -0
- package/templates/skills/deployment-pipeline-design/SKILL.md +359 -0
- package/templates/skills/distributed-tracing/SKILL.md +438 -0
- package/templates/skills/dotnet-backend-patterns/SKILL.md +815 -0
- package/templates/skills/dotnet-backend-patterns/assets/repository-template.cs +523 -0
- package/templates/skills/dotnet-backend-patterns/assets/service-template.cs +336 -0
- package/templates/skills/dotnet-backend-patterns/references/dapper-patterns.md +544 -0
- package/templates/skills/dotnet-backend-patterns/references/ef-core-best-practices.md +355 -0
- package/templates/skills/e2e-testing-patterns/SKILL.md +547 -0
- package/templates/skills/employment-contract-templates/SKILL.md +507 -0
- package/templates/skills/error-handling-patterns/SKILL.md +636 -0
- package/templates/skills/event-store-design/SKILL.md +437 -0
- package/templates/skills/fastapi-templates/SKILL.md +567 -0
- package/templates/skills/git-advanced-workflows/SKILL.md +400 -0
- package/templates/skills/github-actions-templates/SKILL.md +333 -0
- package/templates/skills/go-concurrency-patterns/SKILL.md +655 -0
- package/templates/skills/grafana-dashboards/SKILL.md +369 -0
- package/templates/skills/helm-chart-scaffolding/SKILL.md +544 -0
- package/templates/skills/helm-chart-scaffolding/assets/Chart.yaml.template +42 -0
- package/templates/skills/helm-chart-scaffolding/assets/values.yaml.template +185 -0
- package/templates/skills/helm-chart-scaffolding/references/chart-structure.md +500 -0
- package/templates/skills/helm-chart-scaffolding/scripts/validate-chart.sh +244 -0
- package/templates/skills/javascript-testing-patterns/SKILL.md +1025 -0
- package/templates/skills/langchain-architecture/SKILL.md +338 -0
- package/templates/skills/llm-evaluation/SKILL.md +471 -0
- package/templates/skills/microservices-patterns/SKILL.md +595 -0
- package/templates/skills/modern-javascript-patterns/SKILL.md +911 -0
- package/templates/skills/monorepo-management/SKILL.md +622 -0
- package/templates/skills/nextjs-app-router-patterns/SKILL.md +544 -0
- package/templates/skills/nodejs-backend-patterns/SKILL.md +1020 -0
- package/templates/skills/nx-workspace-patterns/SKILL.md +452 -0
- package/templates/skills/openapi-spec-generation/SKILL.md +1028 -0
- package/templates/skills/paypal-integration/SKILL.md +467 -0
- package/templates/skills/pci-compliance/SKILL.md +466 -0
- package/templates/skills/postgresql/SKILL.md +204 -0
- package/templates/skills/projection-patterns/SKILL.md +490 -0
- package/templates/skills/prometheus-configuration/SKILL.md +392 -0
- package/templates/skills/prompt-engineering-patterns/SKILL.md +201 -0
- package/templates/skills/prompt-engineering-patterns/assets/few-shot-examples.json +106 -0
- package/templates/skills/prompt-engineering-patterns/assets/prompt-template-library.md +246 -0
- package/templates/skills/prompt-engineering-patterns/references/chain-of-thought.md +399 -0
- package/templates/skills/prompt-engineering-patterns/references/few-shot-learning.md +369 -0
- package/templates/skills/prompt-engineering-patterns/references/prompt-optimization.md +414 -0
- package/templates/skills/prompt-engineering-patterns/references/prompt-templates.md +470 -0
- package/templates/skills/prompt-engineering-patterns/references/system-prompts.md +189 -0
- package/templates/skills/prompt-engineering-patterns/scripts/optimize-prompt.py +279 -0
- package/templates/skills/python-packaging/SKILL.md +870 -0
- package/templates/skills/python-performance-optimization/SKILL.md +869 -0
- package/templates/skills/python-testing-patterns/SKILL.md +907 -0
- package/templates/skills/rag-implementation/SKILL.md +403 -0
- package/templates/skills/react-modernization/SKILL.md +513 -0
- package/templates/skills/react-native-architecture/SKILL.md +671 -0
- package/templates/skills/react-state-management/SKILL.md +429 -0
- package/templates/skills/risk-metrics-calculation/SKILL.md +555 -0
- package/templates/skills/rust-async-patterns/SKILL.md +517 -0
- package/templates/skills/secrets-management/SKILL.md +346 -0
- package/templates/skills/security-requirement-extraction/SKILL.md +677 -0
- package/templates/skills/shellcheck-configuration/SKILL.md +454 -0
- package/templates/skills/similarity-search-patterns/SKILL.md +558 -0
- package/templates/skills/slo-implementation/SKILL.md +329 -0
- package/templates/skills/sql-optimization-patterns/SKILL.md +493 -0
- package/templates/skills/stripe-integration/SKILL.md +442 -0
- package/templates/skills/tailwind-design-system/SKILL.md +666 -0
- package/templates/skills/temporal-python-testing/SKILL.md +158 -0
- package/templates/skills/temporal-python-testing/resources/integration-testing.md +455 -0
- package/templates/skills/temporal-python-testing/resources/local-setup.md +553 -0
- package/templates/skills/temporal-python-testing/resources/replay-testing.md +462 -0
- package/templates/skills/temporal-python-testing/resources/unit-testing.md +328 -0
- package/templates/skills/terraform-module-library/SKILL.md +249 -0
- package/templates/skills/terraform-module-library/references/aws-modules.md +63 -0
- package/templates/skills/threat-mitigation-mapping/SKILL.md +745 -0
- package/templates/skills/track-management/SKILL.md +593 -0
- package/templates/skills/typescript-advanced-types/SKILL.md +717 -0
- package/templates/skills/ui-ux-pro-max/SKILL.md +352 -0
- package/templates/skills/ui-ux-pro-max/data/charts.csv +26 -0
- package/templates/skills/ui-ux-pro-max/data/colors.csv +97 -0
- package/templates/skills/ui-ux-pro-max/data/icons.csv +101 -0
- package/templates/skills/ui-ux-pro-max/data/landing.csv +31 -0
- package/templates/skills/ui-ux-pro-max/data/products.csv +97 -0
- package/templates/skills/ui-ux-pro-max/data/prompts.csv +24 -0
- package/templates/skills/ui-ux-pro-max/data/react-performance.csv +45 -0
- package/templates/skills/ui-ux-pro-max/data/styles.csv +59 -0
- package/templates/skills/ui-ux-pro-max/data/typography.csv +58 -0
- package/templates/skills/ui-ux-pro-max/data/ui-reasoning.csv +101 -0
- package/templates/skills/ui-ux-pro-max/data/ux-guidelines.csv +100 -0
- package/templates/skills/ui-ux-pro-max/data/web-interface.csv +31 -0
- package/templates/skills/ui-ux-pro-max/scripts/core.py +258 -0
- package/templates/skills/ui-ux-pro-max/scripts/design_system.py +547 -0
- package/templates/skills/ui-ux-pro-max/scripts/search.py +76 -0
- package/templates/skills/uv-package-manager/SKILL.md +831 -0
- package/templates/skills/vector-index-tuning/SKILL.md +521 -0
- package/templates/skills/wcag-audit-patterns/SKILL.md +555 -0
- package/templates/skills/workflow-orchestration-patterns/SKILL.md +316 -0
- package/templates/skills/workflow-patterns/SKILL.md +623 -0
- package/templates/agents/game-developer.agent.md +0 -57
- package/templates/agents/kubernetes-specialist.agent.md +0 -56
- package/templates/agents/market-researcher.agent.md +0 -47
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: langchain-architecture
|
|
3
|
+
description: Design LLM applications using the LangChain framework with agents, memory, and tool integration patterns. Use when building LangChain applications, implementing AI agents, or creating complex LLM workflows.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# LangChain Architecture
|
|
7
|
+
|
|
8
|
+
Master the LangChain framework for building sophisticated LLM applications with agents, chains, memory, and tool integration.
|
|
9
|
+
|
|
10
|
+
## When to Use This Skill
|
|
11
|
+
|
|
12
|
+
- Building autonomous AI agents with tool access
|
|
13
|
+
- Implementing complex multi-step LLM workflows
|
|
14
|
+
- Managing conversation memory and state
|
|
15
|
+
- Integrating LLMs with external data sources and APIs
|
|
16
|
+
- Creating modular, reusable LLM application components
|
|
17
|
+
- Implementing document processing pipelines
|
|
18
|
+
- Building production-grade LLM applications
|
|
19
|
+
|
|
20
|
+
## Core Concepts
|
|
21
|
+
|
|
22
|
+
### 1. Agents
|
|
23
|
+
Autonomous systems that use LLMs to decide which actions to take.
|
|
24
|
+
|
|
25
|
+
**Agent Types:**
|
|
26
|
+
- **ReAct**: Reasoning + Acting in interleaved manner
|
|
27
|
+
- **OpenAI Functions**: Leverages function calling API
|
|
28
|
+
- **Structured Chat**: Handles multi-input tools
|
|
29
|
+
- **Conversational**: Optimized for chat interfaces
|
|
30
|
+
- **Self-Ask with Search**: Decomposes complex queries
|
|
31
|
+
|
|
32
|
+
### 2. Chains
|
|
33
|
+
Sequences of calls to LLMs or other utilities.
|
|
34
|
+
|
|
35
|
+
**Chain Types:**
|
|
36
|
+
- **LLMChain**: Basic prompt + LLM combination
|
|
37
|
+
- **SequentialChain**: Multiple chains in sequence
|
|
38
|
+
- **RouterChain**: Routes inputs to specialized chains
|
|
39
|
+
- **TransformChain**: Data transformations between steps
|
|
40
|
+
- **MapReduceChain**: Parallel processing with aggregation
|
|
41
|
+
|
|
42
|
+
### 3. Memory
|
|
43
|
+
Systems for maintaining context across interactions.
|
|
44
|
+
|
|
45
|
+
**Memory Types:**
|
|
46
|
+
- **ConversationBufferMemory**: Stores all messages
|
|
47
|
+
- **ConversationSummaryMemory**: Summarizes older messages
|
|
48
|
+
- **ConversationBufferWindowMemory**: Keeps last N messages
|
|
49
|
+
- **EntityMemory**: Tracks information about entities
|
|
50
|
+
- **VectorStoreMemory**: Semantic similarity retrieval
|
|
51
|
+
|
|
52
|
+
### 4. Document Processing
|
|
53
|
+
Loading, transforming, and storing documents for retrieval.
|
|
54
|
+
|
|
55
|
+
**Components:**
|
|
56
|
+
- **Document Loaders**: Load from various sources
|
|
57
|
+
- **Text Splitters**: Chunk documents intelligently
|
|
58
|
+
- **Vector Stores**: Store and retrieve embeddings
|
|
59
|
+
- **Retrievers**: Fetch relevant documents
|
|
60
|
+
- **Indexes**: Organize documents for efficient access
|
|
61
|
+
|
|
62
|
+
### 5. Callbacks
|
|
63
|
+
Hooks for logging, monitoring, and debugging.
|
|
64
|
+
|
|
65
|
+
**Use Cases:**
|
|
66
|
+
- Request/response logging
|
|
67
|
+
- Token usage tracking
|
|
68
|
+
- Latency monitoring
|
|
69
|
+
- Error handling
|
|
70
|
+
- Custom metrics collection
|
|
71
|
+
|
|
72
|
+
## Quick Start
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
from langchain.agents import AgentType, initialize_agent, load_tools
|
|
76
|
+
from langchain.llms import OpenAI
|
|
77
|
+
from langchain.memory import ConversationBufferMemory
|
|
78
|
+
|
|
79
|
+
# Initialize LLM
|
|
80
|
+
llm = OpenAI(temperature=0)
|
|
81
|
+
|
|
82
|
+
# Load tools
|
|
83
|
+
tools = load_tools(["serpapi", "llm-math"], llm=llm)
|
|
84
|
+
|
|
85
|
+
# Add memory
|
|
86
|
+
memory = ConversationBufferMemory(memory_key="chat_history")
|
|
87
|
+
|
|
88
|
+
# Create agent
|
|
89
|
+
agent = initialize_agent(
|
|
90
|
+
tools,
|
|
91
|
+
llm,
|
|
92
|
+
agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION,
|
|
93
|
+
memory=memory,
|
|
94
|
+
verbose=True
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
# Run agent
|
|
98
|
+
result = agent.run("What's the weather in SF? Then calculate 25 * 4")
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Architecture Patterns
|
|
102
|
+
|
|
103
|
+
### Pattern 1: RAG with LangChain
|
|
104
|
+
```python
|
|
105
|
+
from langchain.chains import RetrievalQA
|
|
106
|
+
from langchain.document_loaders import TextLoader
|
|
107
|
+
from langchain.text_splitter import CharacterTextSplitter
|
|
108
|
+
from langchain.vectorstores import Chroma
|
|
109
|
+
from langchain.embeddings import OpenAIEmbeddings
|
|
110
|
+
|
|
111
|
+
# Load and process documents
|
|
112
|
+
loader = TextLoader('documents.txt')
|
|
113
|
+
documents = loader.load()
|
|
114
|
+
|
|
115
|
+
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
|
116
|
+
texts = text_splitter.split_documents(documents)
|
|
117
|
+
|
|
118
|
+
# Create vector store
|
|
119
|
+
embeddings = OpenAIEmbeddings()
|
|
120
|
+
vectorstore = Chroma.from_documents(texts, embeddings)
|
|
121
|
+
|
|
122
|
+
# Create retrieval chain
|
|
123
|
+
qa_chain = RetrievalQA.from_chain_type(
|
|
124
|
+
llm=llm,
|
|
125
|
+
chain_type="stuff",
|
|
126
|
+
retriever=vectorstore.as_retriever(),
|
|
127
|
+
return_source_documents=True
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
# Query
|
|
131
|
+
result = qa_chain({"query": "What is the main topic?"})
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Pattern 2: Custom Agent with Tools
|
|
135
|
+
```python
|
|
136
|
+
from langchain.agents import Tool, AgentExecutor
|
|
137
|
+
from langchain.agents.react.base import ReActDocstoreAgent
|
|
138
|
+
from langchain.tools import tool
|
|
139
|
+
|
|
140
|
+
@tool
|
|
141
|
+
def search_database(query: str) -> str:
|
|
142
|
+
"""Search internal database for information."""
|
|
143
|
+
# Your database search logic
|
|
144
|
+
return f"Results for: {query}"
|
|
145
|
+
|
|
146
|
+
@tool
|
|
147
|
+
def send_email(recipient: str, content: str) -> str:
|
|
148
|
+
"""Send an email to specified recipient."""
|
|
149
|
+
# Email sending logic
|
|
150
|
+
return f"Email sent to {recipient}"
|
|
151
|
+
|
|
152
|
+
tools = [search_database, send_email]
|
|
153
|
+
|
|
154
|
+
agent = initialize_agent(
|
|
155
|
+
tools,
|
|
156
|
+
llm,
|
|
157
|
+
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
|
158
|
+
verbose=True
|
|
159
|
+
)
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### Pattern 3: Multi-Step Chain
|
|
163
|
+
```python
|
|
164
|
+
from langchain.chains import LLMChain, SequentialChain
|
|
165
|
+
from langchain.prompts import PromptTemplate
|
|
166
|
+
|
|
167
|
+
# Step 1: Extract key information
|
|
168
|
+
extract_prompt = PromptTemplate(
|
|
169
|
+
input_variables=["text"],
|
|
170
|
+
template="Extract key entities from: {text}\n\nEntities:"
|
|
171
|
+
)
|
|
172
|
+
extract_chain = LLMChain(llm=llm, prompt=extract_prompt, output_key="entities")
|
|
173
|
+
|
|
174
|
+
# Step 2: Analyze entities
|
|
175
|
+
analyze_prompt = PromptTemplate(
|
|
176
|
+
input_variables=["entities"],
|
|
177
|
+
template="Analyze these entities: {entities}\n\nAnalysis:"
|
|
178
|
+
)
|
|
179
|
+
analyze_chain = LLMChain(llm=llm, prompt=analyze_prompt, output_key="analysis")
|
|
180
|
+
|
|
181
|
+
# Step 3: Generate summary
|
|
182
|
+
summary_prompt = PromptTemplate(
|
|
183
|
+
input_variables=["entities", "analysis"],
|
|
184
|
+
template="Summarize:\nEntities: {entities}\nAnalysis: {analysis}\n\nSummary:"
|
|
185
|
+
)
|
|
186
|
+
summary_chain = LLMChain(llm=llm, prompt=summary_prompt, output_key="summary")
|
|
187
|
+
|
|
188
|
+
# Combine into sequential chain
|
|
189
|
+
overall_chain = SequentialChain(
|
|
190
|
+
chains=[extract_chain, analyze_chain, summary_chain],
|
|
191
|
+
input_variables=["text"],
|
|
192
|
+
output_variables=["entities", "analysis", "summary"],
|
|
193
|
+
verbose=True
|
|
194
|
+
)
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## Memory Management Best Practices
|
|
198
|
+
|
|
199
|
+
### Choosing the Right Memory Type
|
|
200
|
+
```python
|
|
201
|
+
# For short conversations (< 10 messages)
|
|
202
|
+
from langchain.memory import ConversationBufferMemory
|
|
203
|
+
memory = ConversationBufferMemory()
|
|
204
|
+
|
|
205
|
+
# For long conversations (summarize old messages)
|
|
206
|
+
from langchain.memory import ConversationSummaryMemory
|
|
207
|
+
memory = ConversationSummaryMemory(llm=llm)
|
|
208
|
+
|
|
209
|
+
# For sliding window (last N messages)
|
|
210
|
+
from langchain.memory import ConversationBufferWindowMemory
|
|
211
|
+
memory = ConversationBufferWindowMemory(k=5)
|
|
212
|
+
|
|
213
|
+
# For entity tracking
|
|
214
|
+
from langchain.memory import ConversationEntityMemory
|
|
215
|
+
memory = ConversationEntityMemory(llm=llm)
|
|
216
|
+
|
|
217
|
+
# For semantic retrieval of relevant history
|
|
218
|
+
from langchain.memory import VectorStoreRetrieverMemory
|
|
219
|
+
memory = VectorStoreRetrieverMemory(retriever=retriever)
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## Callback System
|
|
223
|
+
|
|
224
|
+
### Custom Callback Handler
|
|
225
|
+
```python
|
|
226
|
+
from langchain.callbacks.base import BaseCallbackHandler
|
|
227
|
+
|
|
228
|
+
class CustomCallbackHandler(BaseCallbackHandler):
|
|
229
|
+
def on_llm_start(self, serialized, prompts, **kwargs):
|
|
230
|
+
print(f"LLM started with prompts: {prompts}")
|
|
231
|
+
|
|
232
|
+
def on_llm_end(self, response, **kwargs):
|
|
233
|
+
print(f"LLM ended with response: {response}")
|
|
234
|
+
|
|
235
|
+
def on_llm_error(self, error, **kwargs):
|
|
236
|
+
print(f"LLM error: {error}")
|
|
237
|
+
|
|
238
|
+
def on_chain_start(self, serialized, inputs, **kwargs):
|
|
239
|
+
print(f"Chain started with inputs: {inputs}")
|
|
240
|
+
|
|
241
|
+
def on_agent_action(self, action, **kwargs):
|
|
242
|
+
print(f"Agent taking action: {action}")
|
|
243
|
+
|
|
244
|
+
# Use callback
|
|
245
|
+
agent.run("query", callbacks=[CustomCallbackHandler()])
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
## Testing Strategies
|
|
249
|
+
|
|
250
|
+
```python
|
|
251
|
+
import pytest
|
|
252
|
+
from unittest.mock import Mock
|
|
253
|
+
|
|
254
|
+
def test_agent_tool_selection():
|
|
255
|
+
# Mock LLM to return specific tool selection
|
|
256
|
+
mock_llm = Mock()
|
|
257
|
+
mock_llm.predict.return_value = "Action: search_database\nAction Input: test query"
|
|
258
|
+
|
|
259
|
+
agent = initialize_agent(tools, mock_llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION)
|
|
260
|
+
|
|
261
|
+
result = agent.run("test query")
|
|
262
|
+
|
|
263
|
+
# Verify correct tool was selected
|
|
264
|
+
assert "search_database" in str(mock_llm.predict.call_args)
|
|
265
|
+
|
|
266
|
+
def test_memory_persistence():
|
|
267
|
+
memory = ConversationBufferMemory()
|
|
268
|
+
|
|
269
|
+
memory.save_context({"input": "Hi"}, {"output": "Hello!"})
|
|
270
|
+
|
|
271
|
+
assert "Hi" in memory.load_memory_variables({})['history']
|
|
272
|
+
assert "Hello!" in memory.load_memory_variables({})['history']
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
## Performance Optimization
|
|
276
|
+
|
|
277
|
+
### 1. Caching
|
|
278
|
+
```python
|
|
279
|
+
from langchain.cache import InMemoryCache
|
|
280
|
+
import langchain
|
|
281
|
+
|
|
282
|
+
langchain.llm_cache = InMemoryCache()
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
### 2. Batch Processing
|
|
286
|
+
```python
|
|
287
|
+
# Process multiple documents in parallel
|
|
288
|
+
from langchain.document_loaders import DirectoryLoader
|
|
289
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
290
|
+
|
|
291
|
+
loader = DirectoryLoader('./docs')
|
|
292
|
+
docs = loader.load()
|
|
293
|
+
|
|
294
|
+
def process_doc(doc):
|
|
295
|
+
return text_splitter.split_documents([doc])
|
|
296
|
+
|
|
297
|
+
with ThreadPoolExecutor(max_workers=4) as executor:
|
|
298
|
+
split_docs = list(executor.map(process_doc, docs))
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
### 3. Streaming Responses
|
|
302
|
+
```python
|
|
303
|
+
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
|
|
304
|
+
|
|
305
|
+
llm = OpenAI(streaming=True, callbacks=[StreamingStdOutCallbackHandler()])
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
## Resources
|
|
309
|
+
|
|
310
|
+
- **references/agents.md**: Deep dive on agent architectures
|
|
311
|
+
- **references/memory.md**: Memory system patterns
|
|
312
|
+
- **references/chains.md**: Chain composition strategies
|
|
313
|
+
- **references/document-processing.md**: Document loading and indexing
|
|
314
|
+
- **references/callbacks.md**: Monitoring and observability
|
|
315
|
+
- **assets/agent-template.py**: Production-ready agent template
|
|
316
|
+
- **assets/memory-config.yaml**: Memory configuration examples
|
|
317
|
+
- **assets/chain-example.py**: Complex chain examples
|
|
318
|
+
|
|
319
|
+
## Common Pitfalls
|
|
320
|
+
|
|
321
|
+
1. **Memory Overflow**: Not managing conversation history length
|
|
322
|
+
2. **Tool Selection Errors**: Poor tool descriptions confuse agents
|
|
323
|
+
3. **Context Window Exceeded**: Exceeding LLM token limits
|
|
324
|
+
4. **No Error Handling**: Not catching and handling agent failures
|
|
325
|
+
5. **Inefficient Retrieval**: Not optimizing vector store queries
|
|
326
|
+
|
|
327
|
+
## Production Checklist
|
|
328
|
+
|
|
329
|
+
- [ ] Implement proper error handling
|
|
330
|
+
- [ ] Add request/response logging
|
|
331
|
+
- [ ] Monitor token usage and costs
|
|
332
|
+
- [ ] Set timeout limits for agent execution
|
|
333
|
+
- [ ] Implement rate limiting
|
|
334
|
+
- [ ] Add input validation
|
|
335
|
+
- [ ] Test with edge cases
|
|
336
|
+
- [ ] Set up observability (callbacks)
|
|
337
|
+
- [ ] Implement fallback strategies
|
|
338
|
+
- [ ] Version control prompts and configurations
|