|
20 | 20 | Genkit framework. It registers Claude models as Genkit actions, enabling |
21 | 21 | text generation operations. |
22 | 22 |
|
23 | | -Key Concepts (ELI5):: |
24 | | -
|
25 | | - ┌─────────────────────┬────────────────────────────────────────────────────┐ |
26 | | - │ Concept │ ELI5 Explanation │ |
27 | | - ├─────────────────────┼────────────────────────────────────────────────────┤ |
28 | | - │ Claude │ Anthropic's AI assistant. Like a helpful friend │ |
29 | | - │ │ who's great at explaining things and writing. │ |
30 | | - ├─────────────────────┼────────────────────────────────────────────────────┤ |
31 | | - │ Sonnet │ The "just right" Claude model. Good at most │ |
32 | | - │ │ tasks without being too slow or expensive. │ |
33 | | - ├─────────────────────┼────────────────────────────────────────────────────┤ |
34 | | - │ Haiku │ The fast & cheap Claude model. Perfect for │ |
35 | | - │ │ quick tasks like classification or summaries. │ |
36 | | - ├─────────────────────┼────────────────────────────────────────────────────┤ |
37 | | - │ Opus │ The most capable Claude. For complex tasks │ |
38 | | - │ │ like research, analysis, or creative writing. │ |
39 | | - ├─────────────────────┼────────────────────────────────────────────────────┤ |
40 | | - │ API Key │ Your password to use Claude. Keep it secret! │ |
41 | | - │ │ Set as ANTHROPIC_API_KEY environment variable. │ |
42 | | - ├─────────────────────┼────────────────────────────────────────────────────┤ |
43 | | - │ System Prompt │ Instructions that shape Claude's personality. │ |
44 | | - │ │ Like giving a new employee their job description. │ |
45 | | - ├─────────────────────┼────────────────────────────────────────────────────┤ |
46 | | - │ Tool Calling │ Claude can use functions you define. Like │ |
47 | | - │ │ giving it a calculator or search engine to use. │ |
48 | | - └─────────────────────┴────────────────────────────────────────────────────┘ |
49 | | -
|
50 | | -Data Flow:: |
51 | | -
|
52 | | - ┌─────────────────────────────────────────────────────────────────────────┐ |
53 | | - │ HOW CLAUDE PROCESSES YOUR REQUEST │ |
54 | | - │ │ |
55 | | - │ Your Code │ |
56 | | - │ ai.generate(prompt="Explain quantum computing") │ |
57 | | - │ │ │ |
58 | | - │ │ (1) Request goes to Anthropic plugin │ |
59 | | - │ ▼ │ |
60 | | - │ ┌─────────────────┐ │ |
61 | | - │ │ Anthropic │ Plugin adds API key to request │ |
62 | | - │ │ Plugin │ │ |
63 | | - │ └────────┬────────┘ │ |
64 | | - │ │ │ |
65 | | - │ │ (2) Converts Genkit format → Claude Messages API │ |
66 | | - │ ▼ │ |
67 | | - │ ┌─────────────────┐ │ |
68 | | - │ │ AnthropicModel │ Handles message roles, images, │ |
69 | | - │ │ │ tools, and streaming │ |
70 | | - │ └────────┬────────┘ │ |
71 | | - │ │ │ |
72 | | - │ │ (3) HTTPS request to api.anthropic.com │ |
73 | | - │ ▼ │ |
74 | | - │ ════════════════════════════════════════════════════ │ |
75 | | - │ │ Internet │ |
76 | | - │ ▼ │ |
77 | | - │ ┌─────────────────┐ │ |
78 | | - │ │ Anthropic │ Claude thinks about your prompt │ |
79 | | - │ │ Claude API │ and generates a response │ |
80 | | - │ └────────┬────────┘ │ |
81 | | - │ │ │ |
82 | | - │ │ (4) Response parsed back to Genkit format │ |
83 | | - │ ▼ │ |
84 | | - │ ┌─────────────────┐ │ |
85 | | - │ │ Your App │ response.text = "Quantum computing..." │ |
86 | | - │ └─────────────────┘ │ |
87 | | - └─────────────────────────────────────────────────────────────────────────┘ |
88 | | -
|
89 | | -Architecture Overview:: |
90 | | -
|
91 | | - ┌─────────────────────────────────────────────────────────────────────────┐ |
92 | | - │ Anthropic Plugin │ |
93 | | - ├─────────────────────────────────────────────────────────────────────────┤ |
94 | | - │ Plugin Entry Point (__init__.py) │ |
95 | | - │ ├── Anthropic - Plugin class │ |
96 | | - │ └── anthropic_name() - Helper to create namespaced model names │ |
97 | | - ├─────────────────────────────────────────────────────────────────────────┤ |
98 | | - │ plugin.py - Plugin Implementation │ |
99 | | - │ ├── Anthropic class (registers models) │ |
100 | | - │ └── Client initialization with Anthropic SDK │ |
101 | | - ├─────────────────────────────────────────────────────────────────────────┤ |
102 | | - │ models.py - Model Implementation │ |
103 | | - │ ├── AnthropicModel (Messages API integration) │ |
104 | | - │ ├── Request/response conversion │ |
105 | | - │ └── Streaming support │ |
106 | | - ├─────────────────────────────────────────────────────────────────────────┤ |
107 | | - │ model_info.py - Model Registry │ |
108 | | - │ ├── SUPPORTED_MODELS (claude-sonnet-4-5, opus, haiku, etc.) │ |
109 | | - │ └── Model capabilities and metadata │ |
110 | | - └─────────────────────────────────────────────────────────────────────────┘ |
111 | | -
|
112 | | -Overview: |
113 | | - The Anthropic plugin adds support for Claude models to Genkit. It uses |
114 | | - the official Anthropic Python SDK and registers models that can be used |
115 | | - with ai.generate() and other Genkit generation methods. |
116 | | -
|
117 | | -Supported Models: |
118 | | - ┌─────────────────────────────────────────────────────────────────────────┐ |
119 | | - │ Model │ Description │ |
120 | | - ├───────────────────────────┼─────────────────────────────────────────────┤ |
121 | | - │ claude-sonnet-4-5 │ Balanced performance and capability │ |
122 | | - │ claude-haiku-4-5 │ Fast and cost-effective │ |
123 | | - │ claude-opus-4-5 │ Most capable, complex tasks │ |
124 | | - │ claude-sonnet-5 │ Latest Sonnet model │ |
125 | | - └───────────────────────────┴─────────────────────────────────────────────┘ |
126 | | -
|
127 | | -Key Components: |
128 | | - ┌─────────────────────────────────────────────────────────────────────────┐ |
129 | | - │ Component │ Purpose │ |
130 | | - ├─────────────────────┼───────────────────────────────────────────────────┤ |
131 | | - │ Anthropic │ Plugin class to register with Genkit │ |
132 | | - │ anthropic_name() │ Helper to create namespaced model names │ |
133 | | - └─────────────────────┴───────────────────────────────────────────────────┘ |
134 | | -
|
135 | 23 | Example: |
136 | | - Basic usage: |
137 | | -
|
138 | 24 | ```python |
139 | 25 | from genkit import Genkit |
140 | 26 | from genkit_anthropic import Anthropic, AnthropicConfig |
141 | 27 |
|
142 | | - # Uses ANTHROPIC_API_KEY env var or pass api_key explicitly |
143 | | - ai = Genkit( |
144 | | - plugins=[Anthropic()], |
145 | | - model='anthropic/claude-sonnet-4-5', |
146 | | - ) |
147 | | -
|
148 | | - response = await ai.generate(prompt='Hello, Claude!') |
149 | | - print(response.text) |
150 | | -
|
151 | | - # With custom configuration |
152 | | - response = await ai.generate( |
153 | | - model='anthropic/claude-haiku-4-5', |
154 | | - prompt='Write a haiku about AI', |
155 | | - config=AnthropicConfig(temperature=0.7, max_output_tokens=100), |
156 | | - ) |
157 | | - ``` |
| 28 | + # 1. Initialize Genkit with the Anthropic plugin |
| 29 | + ai = Genkit(plugins=[Anthropic()]) |
158 | 30 |
|
159 | | - With tools: |
160 | | -
|
161 | | - ```python |
162 | | - @ai.tool() |
163 | | - def get_weather(city: str) -> str: |
164 | | - return f'Weather in {city}: Sunny, 72°F' |
165 | | -
|
166 | | -
|
167 | | - response = await ai.generate( |
| 31 | + # 2. Generate content using Claude Sonnet 4.5 |
| 32 | + res = await ai.generate( |
168 | 33 | model='anthropic/claude-sonnet-4-5', |
169 | | - prompt='What is the weather in Paris?', |
170 | | - tools=['get_weather'], |
| 34 | + prompt='Explain recursion in 10 words.', |
171 | 35 | ) |
| 36 | +
|
| 37 | + # 3. Inspect output shapes directly |
| 38 | + print(res.text) |
| 39 | + # => A function calling itself until reaching a base stopping condition. |
172 | 40 | ``` |
173 | 41 |
|
174 | | -Caveats: |
175 | | - - Requires ANTHROPIC_API_KEY environment variable or api_key parameter |
176 | | - - Model names are prefixed with 'anthropic/' (e.g., 'anthropic/claude-sonnet-4-5') |
177 | | - - Anthropic models may have different tool calling behavior than Google models |
| 42 | +Requirements: |
| 43 | + - Requires the ``ANTHROPIC_API_KEY`` environment variable or explicit ``api_key``. |
178 | 44 |
|
179 | 45 | See Also: |
180 | 46 | - Anthropic documentation: https://docs.anthropic.com/ |
181 | | - - Genkit documentation: https://genkit.dev/ |
182 | 47 | """ |
183 | 48 |
|
184 | 49 | from genkit_anthropic.config import ( |
|
0 commit comments