Skip to content

Commit

Permalink
Merge pull request #2089 from cardstack/support-non-tool-call-models
Browse files Browse the repository at this point in the history
Support non tool call models
  • Loading branch information
IanCal authored Jan 29, 2025
2 parents 21b5571 + ccf69cf commit c8731f2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
24 changes: 23 additions & 1 deletion packages/ai-bot/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const MINIMUM_CREDITS = 10;
class Assistant {
private openai: OpenAI;
private client: MatrixClient;
private toolCallCapableModels: Set<string>;
pgAdapter: PgAdapter;
id: string;

Expand All @@ -57,6 +58,21 @@ class Assistant {
this.id = id;
this.client = client;
this.pgAdapter = new PgAdapter();
this.toolCallCapableModels = new Set();
}

async loadToolCallCapableModels() {
// api request is https://openrouter.ai/api/v1/models?supported_parameters=tools
let response = await fetch(
'https://openrouter.ai/api/v1/models?supported_parameters=tools',
);
let responseJson = (await response.json()) as {
data: { id: string }[];
};
let modelList = responseJson.data;
this.toolCallCapableModels = new Set(
modelList.map((model: any) => model.id),
);
}

async trackAiUsageCost(matrixUserId: string, generationId: string) {
Expand All @@ -72,7 +88,12 @@ class Assistant {
}

getResponse(prompt: PromptParts) {
if (prompt.tools.length === 0) {
// Sending tools to models that don't support them results in an error
// from openrouter.
if (
prompt.tools.length === 0 ||
!this.toolCallCapableModels.has(prompt.model)
) {
return this.openai.beta.chat.completions.stream({
model: prompt.model,
messages: prompt.messages as ChatCompletionMessageParam[],
Expand Down Expand Up @@ -144,6 +165,7 @@ Common issues are:
});
let { user_id: aiBotUserId } = auth;
let assistant = new Assistant(client, aiBotUserId);
await assistant.loadToolCallCapableModels();

client.on(RoomMemberEvent.Membership, function (_event, member) {
if (member.membership === 'invite' && member.userId === aiBotUserId) {
Expand Down
5 changes: 4 additions & 1 deletion packages/host/tests/acceptance/ai-assistant-test.gts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { baseRealm } from '@cardstack/runtime-common';
import {
APP_BOXEL_ACTIVE_LLM,
DEFAULT_LLM,
DEFAULT_LLM_LIST,
} from '@cardstack/runtime-common/matrix-constants';

import {
Expand Down Expand Up @@ -316,7 +317,9 @@ module('Acceptance | AI Assistant tests', function (hooks) {
.hasText(DEFAULT_LLM.split('/')[1]);
await click('[data-test-llm-select-selected]');

assert.dom('[data-test-llm-select-item]').exists({ count: 4 });
assert.dom('[data-test-llm-select-item]').exists({
count: DEFAULT_LLM_LIST.length,
});
assert
.dom('[data-test-llm-select-item="google/gemini-pro-1.5"]')
.hasText('google/gemini-pro-1.5');
Expand Down
2 changes: 2 additions & 0 deletions packages/runtime-common/matrix-constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ export const DEFAULT_LLM_LIST = [
'google/gemini-pro-1.5',
'openai/gpt-4o',
'openai/gpt-4o-mini',
'deepseek/deepseek-r1',
'deepseek/deepseek-r1-distill-llama-70b',
];

0 comments on commit c8731f2

Please sign in to comment.