Skip to content

Commit c17ddba

Browse files
committed
add anthropic 3.5
1 parent 5ee1a96 commit c17ddba

File tree

7 files changed

+126
-23
lines changed

7 files changed

+126
-23
lines changed

components/components/SettingsDialog.tsx

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ function SettingsDialog({ settings, setSettings, openDialog, setOpenDialog }: Pr
9494
<span className="mr-2">OpenAI</span>
9595
<RadioGroupItem value="openai" id="openai-llm"/>
9696
</Label>
97-
<Label className="flex item-center" htmlFor="gemini-llm">
98-
<span className="mr-2">Gemini</span>
99-
<RadioGroupItem value="gemini" id="gemini-llm"/>
97+
<Label className="flex item-center" htmlFor="Anthropic-llm">
98+
<span className="mr-2">Anthropic</span>
99+
<RadioGroupItem value="anthropic" id="Anthropic-llm"/>
100100
</Label>
101101
</RadioGroup>
102102
</div>
@@ -149,26 +149,48 @@ function SettingsDialog({ settings, setSettings, openDialog, setOpenDialog }: Pr
149149

150150
) : (
151151
<>
152-
<p className="text-rose-500">The output effect is not good and it will not be maintained for the time being.</p>
153-
<Label htmlFor="openai-api-key">
154-
<div>Gemini API key</div>
155-
<div className="font-light mt-2 leading-relaxed">
156-
Only stored in your browser. Never stored on servers. Overrides
157-
your .env config.
158-
</div>
159-
</Label>
152+
<Label htmlFor="Anthropic-api-key">
153+
<div>Anthropic API key</div>
154+
<div className="font-light mt-2 leading-relaxed">
155+
Only stored in your browser. Never stored on servers. Overrides
156+
your .env config.
157+
<button
158+
className="inline-flex items-center justify-center ml-2">
159+
<OnboardingNote/>
160+
</button>
161+
</div>
162+
</Label>
160163

161-
<Input
162-
id="Gemini-api-key"
163-
placeholder="Gemini API key"
164-
value={settings?.geminiApiKey || ""}
165-
onChange={(e) =>
166-
setSettings({
167-
...settings,
168-
geminiApiKey: e.target.value,
169-
})
170-
}
171-
/>
164+
<Input
165+
id="anthropic-api-key"
166+
placeholder="Anthropic API key"
167+
value={settings?.anthropicApiKey || ""}
168+
onChange={(e) =>
169+
setSettings({
170+
...settings,
171+
anthropicApiKey: e.target.value,
172+
})
173+
}
174+
/>
175+
176+
<Label htmlFor="anthropic-api-key">
177+
<div>Anthropic Base URL (optional)</div>
178+
<div className="font-light mt-2 leading-relaxed">
179+
Replace with a proxy URL if you don't want to use the default.
180+
</div>
181+
</Label>
182+
183+
<Input
184+
id="anthropic-base-url"
185+
placeholder="Anthropic Base URL"
186+
value={settings?.anthropicBaseURL || ""}
187+
onChange={(e) =>
188+
setSettings({
189+
...settings,
190+
anthropicBaseURL: e.target.value,
191+
})
192+
}
193+
/>
172194
</>
173195
)
174196
}

components/contexts/SettingContext.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ const initialValue = {
2626
init: false,
2727
llm: 'openai',
2828
geminiApiKey: '',
29+
anthropicApiKey: '',
30+
anthropicBaseURL: null
2931
},
3032
initCreate: false,
3133
setSettings: () => {},

components/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export interface Settings {
3232
init: boolean;
3333
llm: string;
3434
geminiApiKey: string;
35+
anthropicApiKey: string;
36+
anthropicBaseURL: string | null;
3537
}
3638

3739
export enum AppState {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"dependencies": {
1212
"@alifd/next": "^1.26.37",
1313
"@alilc/lowcode-datasource-types": "^1.1.4",
14+
"@anthropic-ai/sdk": "^0.24.0",
1415
"@codesandbox/sandpack-react": "^2.11.2",
1516
"@codesandbox/sandpack-themes": "^2.0.21",
1617
"@excalidraw/excalidraw": "0.17.2",

pnpm-lock.yaml

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

service/events/generateCode.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ export interface IGenerateCodeParams {
2121
llm: string;
2222
geminiApiKey: string;
2323
slug?: string;
24+
anthropicApiKey: string;
25+
anthropicBaseURL: string;
2426
}
2527

2628
const encoder = new TextEncoder();
@@ -118,6 +120,8 @@ export async function streamGenerateCode(
118120
openAiBaseURL: params.openAiBaseURL,
119121
llm: params.llm, // 'Gemini'
120122
geminiApiKey: params.geminiApiKey,
123+
anthropicApiKey: params.anthropicApiKey,
124+
anthropicBaseURL: params.anthropicBaseURL
121125
},
122126
);
123127
} catch (e) {

service/events/llm.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import OpenAI from 'openai';
2+
import Anthropic from '@anthropic-ai/sdk';
23

34

45
import {
@@ -133,13 +134,66 @@ export async function streamingOpenAIResponses(
133134
openAiBaseURL: any;
134135
llm: string;
135136
geminiApiKey: any;
137+
anthropicApiKey: any;
138+
anthropicBaseURL: any
136139
}
137140
) {
138-
139141
if (params.llm === "gemini") {
140142
const full_response = await useGeminiResponse([messages, callback, params]);
141143
return full_response;
142144
}
145+
if (params.llm === "anthropic") {
146+
const options: {
147+
apiKey: string;
148+
baseURL?: string;
149+
} = {
150+
apiKey: params.anthropicApiKey, // This is the default and can be omitted
151+
}
152+
if (params.anthropicBaseURL) {
153+
options.baseURL = params.anthropicBaseURL
154+
}
155+
const anthropic = new Anthropic(options);
156+
const systemMessage = messages.splice(0, 1)[0];
157+
const aMessages = messages.map((message: any) => {
158+
const {content} = message;
159+
if (Array.isArray(content)) {
160+
message.content = content.map((item) => {
161+
let temp = item;
162+
// console.log(item.image_url.url)
163+
if (item.type === 'image_url') {
164+
const imageUrl = item.image_url.url
165+
temp = {
166+
type: "image",
167+
source: {
168+
type: "base64",
169+
media_type: imageUrl.split(";")[0].split(":")[1],
170+
data: imageUrl.split(",")[1],
171+
},
172+
}
173+
}
174+
return temp;
175+
})
176+
177+
}
178+
return message;
179+
})
180+
181+
const stream = anthropic.messages
182+
.stream({
183+
model: 'claude-3-5-sonnet-20240620',
184+
max_tokens: 4096,
185+
system: systemMessage.content,
186+
messages: aMessages
187+
})
188+
.on('text', (text) => {
189+
callback(text);
190+
});
191+
192+
const message = await stream.finalMessage();
193+
return message;
194+
}
195+
196+
143197

144198
if (!params.openAiApiKey) {
145199
callback('No openai key, set it', 'error');

0 commit comments

Comments
 (0)