Skip to content

Commit 0c50e6a

Browse files
author
Your a
committed
fetch openrouter models
1 parent 222b9b2 commit 0c50e6a

File tree

4 files changed

+50
-17
lines changed

4 files changed

+50
-17
lines changed

.vscode/launch.json

+13-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@
1111
"port": 9222,
1212
"webRoot": "${workspaceFolder}",
1313
"timeout": 30000
14-
}
14+
},
15+
{
16+
"type": "node",
17+
"request": "launch",
18+
"name": "Run Development Server",
19+
"runtimeExecutable": "npm",
20+
"runtimeArgs": [
21+
"run",
22+
"dev"
23+
],
24+
"port": 3000,
25+
"timeout": 30000
26+
}
1527
]
1628
}

app/components/chat/Chat.client.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export const ChatImpl = memo(({ initialMessages, storeMessageHistory }: ChatProp
9292
},
9393
initialMessages,
9494
body: {
95-
model: provider
95+
provider: provider
9696
},
9797
});
9898

+33-13
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,57 @@
1-
import React, { useState } from 'react';
1+
import React, { useState, useEffect } from 'react';
22
import { useStore } from '@nanostores/react';
33
import { providerStore, setProvider, type Provider } from '~/lib/stores/provider';
44
import '~/styles/index.scss';
55

66
export function ProviderSelector() {
77
const currentProvider = useStore(providerStore);
88
const [inputValue, setInputValue] = useState(currentProvider.model);
9+
const [providers, setProviders] = useState<string[]>([]);
10+
const [apiKey, setApiKey] = useState(''); // New state for API key
11+
12+
useEffect(() => {
13+
const fetchProviders = async () => {
14+
try {
15+
const response = await fetch('https://openrouter.ai/api/v1/models');
16+
const data = await response.json();
17+
setProviders(data.data.map(a=>a.id));
18+
} catch (error) {
19+
console.error('Error fetching providers:', error);
20+
}
21+
};
22+
23+
fetchProviders();
24+
}, []);
925

1026
const handleProviderChange = (event: React.ChangeEvent<HTMLInputElement>) => {
1127
const value = event.target.value;
1228
setInputValue(value);
1329
};
1430

15-
const providers = [
16-
'Anthropic (Claude)',
17-
'Together AI (meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo)',
18-
'Together AI (meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo)',
19-
'Together AI (mistralai/Mixtral-8x7B-Instruct-v0.1)',
20-
'OpenRouter (openai/gpt-3.5-turbo)',
21-
'OpenRouter (anthropic/claude-2)',
22-
'OpenRouter (google/palm-2-chat-bison)',
23-
];
31+
const handleApiKeyButtonClick = () => {
32+
const value = prompt("Enter OpenRouter API Key"); // Prompt user for API key
33+
if (value) {
34+
setApiKey(value); // Update API key state if a value is provided
35+
}
36+
};
2437

2538
return (
26-
<div className="relative w-[250px]">
39+
<div className="flex space-x-2"> {/* Changed to flex for horizontal layout */}
40+
{/* Button to set API key */}
41+
<button
42+
onClick={handleApiKeyButtonClick}
43+
className="w-[120px] px-3 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 focus:outline-none"
44+
>
45+
Set API Key
46+
</button>
2747
<input
2848
list="providers"
2949
value={inputValue}
3050
onChange={handleProviderChange}
3151
onMouseDown={() => {
3252
setInputValue(''); // Clear input on click
3353
}}
34-
className="w-full px-3 py-2 bg-transparent border border-white/20 rounded text-white hover:border-white/40 focus:border-white/60 outline-none"
54+
className="w-[300px] px-3 py-2 bg-transparent border border-white/20 rounded text-white hover:border-white/40 focus:border-white/60 outline-none" // Increased width for larger input
3555
/>
3656
<datalist id="providers">
3757
{providers.map((provider) => (
@@ -40,4 +60,4 @@ export function ProviderSelector() {
4060
</datalist>
4161
</div>
4262
);
43-
}
63+
}

app/lib/.server/llm/stream-text.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ export type StreamingOptions = Omit<Parameters<typeof _streamText>[0], 'model'>;
2424

2525

2626
export function streamText(messages: Messages, env: Env, provider: Provider, options?: StreamingOptions) {
27-
const apiKey = getAPIKeys(env);
2827

28+
const apiKey = getAPIKeys(env);
29+
const apiKeyToUse = provider.apiKey || apiKey.openRouter;
2930
return _streamText({
30-
model: getModel('openrouter', provider.apiKey || apiKey.openRouter, provider.model),
31+
model: getModel('openrouter', apiKeyToUse, provider.model),
3132

3233
system: getSystemPrompt(),
3334
messages: convertToCoreMessages(messages.map(message => ({

0 commit comments

Comments
 (0)