-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathdoc.go
More file actions
256 lines (256 loc) · 7.17 KB
/
doc.go
File metadata and controls
256 lines (256 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Package langchaingo provides a Go implementation of LangChain, a framework for building applications with Large Language Models (LLMs) through composability.
//
// LangchainGo enables developers to create powerful AI-driven applications by providing a unified interface to various LLM providers, vector databases, and other AI services.
// The framework emphasizes modularity, extensibility, and ease of use.
//
// # Core Components
//
// The framework is organized around several key packages:
//
// - [github.com/tmc/langchaingo/llms]: Interfaces and implementations for various language models (OpenAI, Anthropic, Google, etc.)
// - [github.com/tmc/langchaingo/chains]: Composable operations that can be linked together to create complex workflows
// - [github.com/tmc/langchaingo/agents]: Autonomous entities that can use tools to accomplish tasks
// - [github.com/tmc/langchaingo/embeddings]: Text embedding functionality for semantic search and similarity
// - [github.com/tmc/langchaingo/vectorstores]: Interfaces to vector databases for storing and querying embeddings
// - [github.com/tmc/langchaingo/memory]: Conversation history and context management
// - [github.com/tmc/langchaingo/tools]: External tool integrations (web search, calculators, databases, etc.)
//
// # Quick Start
//
// Basic text generation with OpenAI:
//
// import (
// "context"
// "log"
//
// "github.com/tmc/langchaingo/llms"
// "github.com/tmc/langchaingo/llms/openai"
// )
//
// ctx := context.Background()
// llm, err := openai.New()
// if err != nil {
// log.Fatal(err)
// }
//
// completion, err := llm.GenerateContent(ctx, []llms.MessageContent{
// llms.TextParts(llms.ChatMessageTypeHuman, "What is the capital of France?"),
// })
//
// Creating embeddings and using vector search:
//
// import (
// "github.com/tmc/langchaingo/embeddings"
// "github.com/tmc/langchaingo/schema"
// "github.com/tmc/langchaingo/vectorstores/chroma"
// )
//
// // Create an embedder
// embedder, err := embeddings.NewEmbedder(llm)
// if err != nil {
// log.Fatal(err)
// }
//
// // Create a vector store
// store, err := chroma.New(
// chroma.WithChromaURL("http://localhost:8000"),
// chroma.WithEmbedder(embedder),
// )
//
// // Add documents
// docs := []schema.Document{
// {PageContent: "Paris is the capital of France"},
// {PageContent: "London is the capital of England"},
// }
// store.AddDocuments(ctx, docs)
//
// // Search for similar documents
// results, err := store.SimilaritySearch(ctx, "French capital", 1)
//
// Building a chain for question answering:
//
// import (
// "github.com/tmc/langchaingo/chains"
// "github.com/tmc/langchaingo/vectorstores"
// )
//
// chain := chains.NewRetrievalQAFromLLM(
// llm,
// vectorstores.ToRetriever(store, 3),
// )
//
// answer, err := chains.Run(ctx, chain, "What is the capital of France?")
//
// # Provider Support
//
// LangchainGo supports numerous providers:
//
// LLM Providers:
// - OpenAI (GPT-3.5, GPT-4, GPT-4 Turbo)
// - Anthropic (Claude family)
// - Google AI (Gemini, PaLM)
// - AWS Bedrock (Claude, Llama, Titan)
// - Cohere
// - Mistral AI
// - Ollama (local models)
// - Hugging Face Inference
// - And many more...
//
// Embedding Providers:
// - OpenAI
// - Hugging Face
// - Jina AI
// - Voyage AI
// - Google Vertex AI
// - AWS Bedrock
//
// Vector Stores:
// - Chroma
// - Pinecone
// - Weaviate
// - Qdrant
// - PostgreSQL with pgvector
// - Redis
// - Milvus
// - MongoDB Atlas Vector Search
// - OpenSearch
// - Azure AI Search
//
// # Agents and Tools
//
// Create agents that can use tools to accomplish complex tasks:
//
// import (
// "github.com/tmc/langchaingo/agents"
// "github.com/tmc/langchaingo/tools/serpapi"
// "github.com/tmc/langchaingo/tools/calculator"
// )
//
// // Create tools
// searchTool := serpapi.New("your-api-key")
// calcTool := calculator.New()
//
// // Create an agent
// agent := agents.NewMRKLAgent(llm, []tools.Tool{searchTool, calcTool})
// executor := agents.NewExecutor(agent)
//
// // Run the agent
// result, err := executor.Call(ctx, map[string]any{
// "input": "What's the current population of Tokyo multiplied by 2?",
// })
//
// # Memory and Conversation
//
// Maintain conversation context across multiple interactions:
//
// import (
// "github.com/tmc/langchaingo/memory"
// "github.com/tmc/langchaingo/chains"
// )
//
// // Create memory
// memory := memory.NewConversationBuffer()
//
// // Create a conversation chain
// chain := chains.NewConversation(llm, memory)
//
// // Have a conversation
// chains.Run(ctx, chain, "Hello, my name is Alice")
// chains.Run(ctx, chain, "What's my name?") // Will remember "Alice"
//
// # Advanced Features
//
// Streaming responses:
//
// stream, err := llm.GenerateContentStream(ctx, messages)
// for stream.Next() {
// chunk := stream.Value()
// fmt.Print(chunk.Choices[0].Content)
// }
//
// Function calling:
//
// tools := []llms.Tool{
// {
// Type: "function",
// Function: &llms.FunctionDefinition{
// Name: "get_weather",
// Parameters: map[string]any{
// "type": "object",
// "properties": map[string]any{
// "location": map[string]any{"type": "string"},
// },
// },
// },
// },
// }
//
// content, err := llm.GenerateContent(ctx, messages, llms.WithTools(tools))
//
// Multi-modal inputs (text and images):
//
// parts := []llms.ContentPart{
// llms.TextPart("What's in this image?"),
// llms.ImagePart("data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ..."),
// }
// content, err := llm.GenerateContent(ctx, []llms.MessageContent{
// {Role: llms.ChatMessageTypeHuman, Parts: parts},
// })
//
// # Configuration and Environment
//
// Most providers require API keys set as environment variables:
//
// export OPENAI_API_KEY="your-openai-key"
// export ANTHROPIC_API_KEY="your-anthropic-key"
// export GOOGLE_API_KEY="your-google-key"
// export HUGGINGFACEHUB_API_TOKEN="your-hf-token"
//
// # Error Handling
//
// LangchainGo provides standardized error handling:
//
// import "github.com/tmc/langchaingo/llms"
//
// if err != nil {
// if llms.IsAuthenticationError(err) {
// log.Fatal("Invalid API key")
// }
// if llms.IsRateLimitError(err) {
// log.Println("Rate limited, retrying...")
// }
// }
//
// # Testing
//
// LangchainGo includes comprehensive testing utilities including HTTP record/replay for internal tests.
// The httprr package provides deterministic testing of HTTP interactions:
//
// import "github.com/tmc/langchaingo/internal/httprr"
//
// func TestMyFunction(t *testing.T) {
// rr := httprr.OpenForTest(t, http.DefaultTransport)
// defer rr.Close()
//
// client := rr.Client()
// // Use client for HTTP requests - they'll be recorded/replayed for deterministic testing
// }
//
// # Examples
//
// See the examples/ directory for complete working examples including:
// - Basic LLM usage
// - RAG (Retrieval Augmented Generation)
// - Agent workflows
// - Vector database integration
// - Multi-modal applications
// - Streaming responses
// - Function calling
//
// # Contributing
//
// LangchainGo welcomes contributions! The project follows Go best practices
// and includes comprehensive testing, linting, and documentation standards.
//
// See CONTRIBUTING.md for detailed guidelines.
package langchaingo