Skip to content

Commit

Permalink
Merge pull request #164 from sugarforever/bugfix/issue-161-azure-mode…
Browse files Browse the repository at this point in the history
…ls-selection

extend model identification to model + family
  • Loading branch information
sugarforever authored Mar 28, 2024
2 parents 8d79131 + fecf920 commit 6552dbe
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
12 changes: 11 additions & 1 deletion components/Chat.vue
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,22 @@ const onSend = async (data: ChatBoxFormData) => {
type: 'loading'
})
let modelObject = null;
try {
modelObject = JSON.parse(model.value);
} catch (e) {
console.error("Invalid model storage: ", e);
}
const body = JSON.stringify({
knowledgebaseId: props.knowledgebase?.id,
model: model.value,
model: modelObject?.model,
family: modelObject?.family,
messages: [...messages.value.filter(m => m.type !== 'loading')],
stream: true,
})
const controller = new AbortController();
abortHandler = () => controller.abort();
await fetchStream('/api/models/chat', {
Expand Down
7 changes: 6 additions & 1 deletion components/ModelsDropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ const loadModels = async () => {
// filter out nomic-bert family models,as they as embedding models do not support chat apparently.
.filter(el => el?.details?.family !== 'nomic-bert')
.map(el => {
return { label: `${el?.details?.family === "Azure OpenAI" ? `Azure ${el.name}` : el.name}`, value: el.name }
return {
label: `${el?.details?.family === "Azure OpenAI" ? `Azure ${el.name}` : el.name}`, value: JSON.stringify({
model: el.name,
family: el?.details?.family
})
}
})
if (models.value.length === 0) return
Expand Down
6 changes: 3 additions & 3 deletions server/api/models/chat/index.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ If the context doesn't contain any relevant information to the question, don't m
`;

export default defineEventHandler(async (event) => {
const { knowledgebaseId, model, messages, stream } = await readBody(event);
const { knowledgebaseId, model, family, messages, stream } = await readBody(event);

if (knowledgebaseId) {
console.log("Chat with knowledge base with id: ", knowledgebaseId);
Expand All @@ -44,7 +44,7 @@ export default defineEventHandler(async (event) => {
new MessagesPlaceholder("messages"),
]);

const chat = createChatModel(model, event);
const chat = createChatModel(model, family, event);

const query = messages[messages.length - 1].content
console.log("User query: ", query);
Expand Down Expand Up @@ -99,7 +99,7 @@ export default defineEventHandler(async (event) => {
})());
return sendStream(event, readableStream);
} else {
const llm = createChatModel(model, event);
const llm = createChatModel(model, family, event);
const response = await llm?.stream(messages.map((message) => {
return [message.role, message.content];
}));
Expand Down
10 changes: 5 additions & 5 deletions server/api/models/index.get.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type ModelResponse, type ModelDetails } from 'ollama'
import { OPENAI_GPT_MODELS, ANTHROPIC_MODELS, AZURE_OPENAI_GPT_MODELS, MOONSHOT_MODELS } from '@/server/utils/models';
import { MODEL_FAMILIES, OPENAI_GPT_MODELS, ANTHROPIC_MODELS, AZURE_OPENAI_GPT_MODELS, MOONSHOT_MODELS } from '@/server/utils/models';
import { getOllama } from '@/server/utils/ollama'

export interface ModelItem extends Partial<Omit<ModelResponse, 'details'>> {
Expand Down Expand Up @@ -32,7 +32,7 @@ export default defineEventHandler(async (event) => {
models.push({
name: model,
details: {
family: 'OpenAI'
family: MODEL_FAMILIES.openai
}
});
});
Expand All @@ -43,7 +43,7 @@ export default defineEventHandler(async (event) => {
models.push({
name: model,
details: {
family: 'Azure OpenAI'
family: MODEL_FAMILIES.azureOpenai
}
});
});
Expand All @@ -54,7 +54,7 @@ export default defineEventHandler(async (event) => {
models.push({
name: model,
details: {
family: 'Anthropic'
family: MODEL_FAMILIES.anthropic
}
});
});
Expand All @@ -65,7 +65,7 @@ export default defineEventHandler(async (event) => {
models.push({
name: model,
details: {
family: 'Moonshot'
family: MODEL_FAMILIES.moonshot
}
});
});
Expand Down
17 changes: 12 additions & 5 deletions server/utils/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import { AzureChatOpenAI } from "@langchain/azure-openai";
import { type H3Event } from 'h3'
import { type KEYS } from '@/server/middleware/keys'

export const MODEL_FAMILIES = {
openai: 'OpenAI',
azureOpenai: 'Azure OpenAI',
anthropic: 'Anthropic',
moonshot: 'Moonshot',
};

export const OPENAI_GPT_MODELS = [
"gpt-3.5-turbo",
"gpt-4",
Expand Down Expand Up @@ -74,11 +81,11 @@ export const createEmbeddings = (embeddingModelName: string, event: H3Event): Em
}
}

export const createChatModel = (modelName: string, event: H3Event): BaseChatModel => {
export const createChatModel = (modelName: string, family: string, event: H3Event): BaseChatModel => {
const { host } = event.context.ollama;
const keys = event.context.keys as Record<KEYS, string>;
let chat = null;
if (OPENAI_GPT_MODELS.includes(modelName)) {
if (family === MODEL_FAMILIES.openai && OPENAI_GPT_MODELS.includes(modelName)) {
console.log("Chat with OpenAI, host:", keys.x_openai_api_host);
chat = new ChatOpenAI({
configuration: {
Expand All @@ -87,22 +94,22 @@ export const createChatModel = (modelName: string, event: H3Event): BaseChatMode
openAIApiKey: keys.x_openai_api_key,
modelName: modelName
})
} else if (AZURE_OPENAI_GPT_MODELS.includes(modelName)) {
} else if (family === MODEL_FAMILIES.azureOpenai && AZURE_OPENAI_GPT_MODELS.includes(modelName)) {
console.log(`Chat with Azure OpenAI endpoint: ${keys.x_azure_openai_endpoint} , deployment: ${keys.x_azure_openai_deployment_name}`);
chat = new AzureChatOpenAI({
azureOpenAIEndpoint: keys.x_azure_openai_endpoint,
azureOpenAIApiKey: keys.x_azure_openai_api_key,
azureOpenAIApiDeploymentName: keys.x_azure_openai_deployment_name,
modelName: modelName,
})
} else if (ANTHROPIC_MODELS.includes(modelName)) {
} else if (family === MODEL_FAMILIES.anthropic && ANTHROPIC_MODELS.includes(modelName)) {
console.log("Chat with Anthropic, host:", keys.x_anthropic_api_host);
chat = new ChatAnthropic({
anthropicApiUrl: keys.x_anthropic_api_host || undefined,
anthropicApiKey: keys.x_anthropic_api_key,
modelName: modelName
})
} else if (MOONSHOT_MODELS.includes(modelName)) {
} else if (family === MODEL_FAMILIES.moonshot && MOONSHOT_MODELS.includes(modelName)) {
// Reuse openai's sdk
console.log("Chat with Moonshot, host:", keys.x_moonshot_api_host);
chat = new ChatOpenAI({
Expand Down

0 comments on commit 6552dbe

Please sign in to comment.