Skip to content

Commit c4c10b2

Browse files
committed
feat: implement Anthropic API integration with message handling and code generation examples
1 parent dda8669 commit c4c10b2

3 files changed

Lines changed: 460 additions & 11 deletions

File tree

src/client/components/aiGateway/AIGatewayCodeExampleBtn.tsx

Lines changed: 82 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,10 @@ export const AIGatewayCodeExampleBtn: React.FC<AIGatewayCodeExampleBtnProps> =
8686
[gatewayId]
8787
);
8888

89-
// Generate base code template
90-
const generateCodeTemplate = (provider: AIProvider, language: string) => {
91-
const config = providerConfigs[provider];
92-
const baseUrl = config.baseUrl
93-
.replace('${workspaceId}', workspaceId)
94-
.replace('${gatewayId}', gatewayId);
95-
const model = config.defaultModel;
89+
const isAnthropicNative = (provider: AIProvider) =>
90+
provider === 'anthropic';
9691

97-
// Generate different code based on language
92+
const generateOpenAITemplate = (baseUrl: string, model: string, language: string) => {
9893
switch (language) {
9994
case 'nodejs':
10095
return `const axios = require('axios');
@@ -189,9 +184,86 @@ call_ai_gateway()`;
189184
}
190185
};
191186

192-
// Generate code examples for different providers and languages
187+
const generateAnthropicTemplate = (baseUrl: string, model: string, language: string) => {
188+
switch (language) {
189+
case 'nodejs':
190+
return `const Anthropic = require('@anthropic-ai/sdk');
191+
192+
const client = new Anthropic({
193+
apiKey: '<YOUR_API_KEY>',
194+
baseURL: '${window.location.origin}${baseUrl}',
195+
});
196+
197+
async function callAIGateway() {
198+
const message = await client.messages.create({
199+
model: '${model}',
200+
max_tokens: 1024,
201+
system: 'You are an AI assistant',
202+
messages: [
203+
{ role: 'user', content: 'Hello' }
204+
],
205+
});
206+
console.log(message.content);
207+
}
208+
209+
callAIGateway();
210+
211+
// Claude Code: set ANTHROPIC_BASE_URL=${window.location.origin}${baseUrl}`;
212+
213+
case 'python':
214+
return `import anthropic
215+
216+
client = anthropic.Anthropic(
217+
api_key="<YOUR_API_KEY>",
218+
base_url="${window.location.origin}${baseUrl}",
219+
)
220+
221+
message = client.messages.create(
222+
model="${model}",
223+
max_tokens=1024,
224+
system="You are an AI assistant",
225+
messages=[
226+
{"role": "user", "content": "Hello"}
227+
],
228+
)
229+
230+
print(message.content)
231+
232+
# Claude Code: set ANTHROPIC_BASE_URL=${window.location.origin}${baseUrl}`;
233+
234+
case 'curl':
235+
return `curl -X POST '${window.location.origin}${baseUrl}/v1/messages' \\
236+
-H 'Content-Type: application/json' \\
237+
-H 'x-api-key: <YOUR_API_KEY>' \\
238+
-H 'anthropic-version: 2023-06-01' \\
239+
-d '{
240+
"model": "${model}",
241+
"max_tokens": 1024,
242+
"system": "You are an AI assistant",
243+
"messages": [
244+
{
245+
"role": "user",
246+
"content": "Hello"
247+
}
248+
]
249+
}'`;
250+
251+
default:
252+
return '';
253+
}
254+
};
255+
193256
const generateCode = (provider: AIProvider, language: string) => {
194-
return generateCodeTemplate(provider, language);
257+
const config = providerConfigs[provider];
258+
const baseUrl = config.baseUrl
259+
.replace('${workspaceId}', workspaceId)
260+
.replace('${gatewayId}', gatewayId);
261+
const model = config.defaultModel;
262+
263+
if (isAnthropicNative(provider)) {
264+
return generateAnthropicTemplate(baseUrl, model, language);
265+
}
266+
return generateOpenAITemplate(baseUrl, model, language);
195267
};
196268

197269
const currentConfig = providerConfigs[selectedProvider];

0 commit comments

Comments
 (0)