Skip to content

Commit

Permalink
Merge pull request sugarforever#182 from sugarforever/feature/groq
Browse files Browse the repository at this point in the history
feat: support Groq API
  • Loading branch information
sugarforever authored Mar 31, 2024
2 parents b0e6841 + ffde9cb commit 2ab6c51
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 9 deletions.
1 change: 0 additions & 1 deletion components/Chat.vue
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ function onAbortChat() {
<div class="flex flex-col flex-1 box-border dark:text-gray-300 -mx-4">
<div class="flex items-center justify-between mb-4 px-4 shrink-0">
<div class="flex items-center">
<span class="mr-2">Chat with</span>
<ModelsDropdown v-model="model" placeholder="Select a model" />
</div>
<div>
Expand Down
5 changes: 3 additions & 2 deletions components/ModelsDropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const loadModels = async () => {
.filter(el => el?.details?.family !== 'nomic-bert')
.map(el => {
return {
label: `${el?.details?.family === "Azure OpenAI" ? `Azure ${el.name}` : el.name}`, value: JSON.stringify({
label: `${el?.details?.family} ${el.name}`,
value: JSON.stringify({
model: el.name,
family: el?.details?.family
})
Expand All @@ -45,6 +46,6 @@ loadModels()
<template>
<ClientOnly>
<USelect v-model="currentModel" :options="models" :placeholder="placeholder" />
<USelect v-model="currentModel" size="sm" :options="models" :placeholder="placeholder" />
</ClientOnly>
</template>
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@langchain/community": "^0.0.33",
"@langchain/core": "^0.1.44",
"@langchain/google-genai": "^0.0.10",
"@langchain/groq": "^0.0.5",
"@langchain/openai": "^0.0.15",
"@nuxt/ui": "^2.14.2",
"@prisma/client": "^5.10.2",
Expand Down
18 changes: 17 additions & 1 deletion pages/settings/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
moonshotApiHost,
geminiApiKey,
groqApiKey,
} from '@/utils/settings';
const toast = useToast();
Expand All @@ -39,7 +41,9 @@ const state = reactive({
moonshotApiKey: undefined,
moonshotApiHost: undefined,
geminiApiKey: undefined
geminiApiKey: undefined,
groqApiKey: undefined,
});
const saving = ref(false);
Expand Down Expand Up @@ -76,6 +80,8 @@ const onSubmit = async () => {
geminiApiKey.value = state.geminiApiKey;
groqApiKey.value = state.groqApiKey;
toast.add({ title: `Set successfully!` });
};
Expand Down Expand Up @@ -105,6 +111,9 @@ onMounted(() => {
// Gemini
state.geminiApiKey = geminiApiKey.value;
// Groq
state.groqApiKey = groqApiKey.value;
authorization.value = !!(state.username && state.password);
});
Expand Down Expand Up @@ -197,6 +206,13 @@ const ui = {
</UFormGroup>
</UCard>

<UCard :ui="ui">
<template #header>Groq</template>
<UFormGroup label="API Key" name="groqApiKey" class="mb-4">
<UInput v-model="state.groqApiKey" type="password" />
</UFormGroup>
</UCard>

<div class="">
<UButton type="submit" :loading="saving">
Save
Expand Down
43 changes: 43 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion server/api/models/index.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ export default defineEventHandler(async (event) => {
x_moonshot_api_key: moonshot_api_key,
x_moonshot_api_host: moonshot_api_host,

x_gemini_api_key: gemini_api_key
x_gemini_api_key: gemini_api_key,

x_groq_api_key: groq_api_key,
} = event.context.keys;
const models: ModelItem[] = []

Expand Down Expand Up @@ -84,5 +86,16 @@ export default defineEventHandler(async (event) => {
});
}

if (groq_api_key) {
GROQ_MODELS.forEach((model) => {
models.push({
name: model,
details: {
family: MODEL_FAMILIES.groq
}
});
});
}

return models
})
4 changes: 3 additions & 1 deletion server/middleware/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ const KEYS = [
'x_moonshot_api_key',
'x_moonshot_api_host',

'x_gemini_api_key'
'x_gemini_api_key',

'x_groq_api_key'
] as const;

export type KEYS = typeof KEYS[number];
Expand Down
18 changes: 16 additions & 2 deletions server/utils/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ChatAnthropic } from "@langchain/anthropic";
import { ChatOllama } from "@langchain/community/chat_models/ollama";
import { ChatOpenAI } from '@langchain/openai';
import { ChatGoogleGenerativeAI } from "@langchain/google-genai";
import { ChatGroq } from "@langchain/groq";
import { AzureChatOpenAI } from "@langchain/azure-openai";
import { type H3Event } from 'h3'
import { type KEYS } from '@/server/middleware/keys'
Expand All @@ -16,7 +17,8 @@ export const MODEL_FAMILIES = {
azureOpenai: 'Azure OpenAI',
anthropic: 'Anthropic',
moonshot: 'Moonshot',
gemini: 'Gemini'
gemini: 'Gemini',
groq: 'Groq'
};

export const OPENAI_GPT_MODELS = [
Expand Down Expand Up @@ -62,7 +64,13 @@ export const MOONSHOT_MODELS = [
export const GEMINI_MODELS = [
"gemini-1.0-pro",
"gemini-1.0-pro-vision-latest"
]
];

export const GROQ_MODELS = [
"llama2-70b-4096",
"mixtral-8x7b-32768",
"gemma-7b-it"
];

export const isOllamaModelExists = async (ollama: Ollama, embeddingModelName: string) => {
if (!OPENAI_EMBEDDING_MODELS.includes(embeddingModelName) && !GEMINI_EMBEDDING_MODELS.includes(embeddingModelName)) {
Expand Down Expand Up @@ -144,6 +152,12 @@ export const createChatModel = (modelName: string, family: string, event: H3Even
apiKey: keys.x_gemini_api_key,
modelName: modelName
})
} else if (family === MODEL_FAMILIES.groq && GROQ_MODELS.includes(modelName)) {
console.log(`Chat with Groq ${modelName}`);
chat = new ChatGroq({
apiKey: keys.x_groq_api_key,
modelName: modelName
})
} else {
console.log("Chat with Ollama, host:", host);
chat = new ChatOllama({
Expand Down
8 changes: 7 additions & 1 deletion utils/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const MOONSHOT_API_HOST = 'keys.moonshot_api_host';

const GEMINI_API_KEY = "keys.gemini_api_key";

const GROQ_API_KEY = "keys.groq_api_key";

export const ollamaHost = useStorage(OLLAMA_HOST, '');
export const ollamaUsername = useStorage(OLLAMA_USERNAME, '');
export const ollamaPassword = useStorage(OLLAMA_PASSWORD, '');
Expand All @@ -39,6 +41,8 @@ export const moonshotApiHost = useStorage(MOONSHOT_API_HOST, '');

export const geminiApiKey = useStorage(GEMINI_API_KEY, '');

export const groqApiKey = useStorage(GROQ_API_KEY, '');

export const fetchHeadersOllama = computed(() => {
return {
'x_ollama_host': ollamaHost.value,
Expand All @@ -62,7 +66,9 @@ export const fetchHeadersThirdApi = computed(() => {
'x_moonshot_api_key': moonshotApiKey.value,
'x_moonshot_api_host': moonshotApiHost.value,

'x_gemini_api_key': geminiApiKey.value
'x_gemini_api_key': geminiApiKey.value,

'x_groq_api_key': groqApiKey.value,
}
})

Expand Down

0 comments on commit 2ab6c51

Please sign in to comment.