Skip to content

Commit c8731f2

Browse files
authoredJan 29, 2025
Merge pull request #2089 from cardstack/support-non-tool-call-models
Support non tool call models
2 parents 21b5571 + ccf69cf commit c8731f2

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed
 

Diff for: ‎packages/ai-bot/main.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const MINIMUM_CREDITS = 10;
4646
class Assistant {
4747
private openai: OpenAI;
4848
private client: MatrixClient;
49+
private toolCallCapableModels: Set<string>;
4950
pgAdapter: PgAdapter;
5051
id: string;
5152

@@ -57,6 +58,21 @@ class Assistant {
5758
this.id = id;
5859
this.client = client;
5960
this.pgAdapter = new PgAdapter();
61+
this.toolCallCapableModels = new Set();
62+
}
63+
64+
async loadToolCallCapableModels() {
65+
// api request is https://openrouter.ai/api/v1/models?supported_parameters=tools
66+
let response = await fetch(
67+
'https://openrouter.ai/api/v1/models?supported_parameters=tools',
68+
);
69+
let responseJson = (await response.json()) as {
70+
data: { id: string }[];
71+
};
72+
let modelList = responseJson.data;
73+
this.toolCallCapableModels = new Set(
74+
modelList.map((model: any) => model.id),
75+
);
6076
}
6177

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

7490
getResponse(prompt: PromptParts) {
75-
if (prompt.tools.length === 0) {
91+
// Sending tools to models that don't support them results in an error
92+
// from openrouter.
93+
if (
94+
prompt.tools.length === 0 ||
95+
!this.toolCallCapableModels.has(prompt.model)
96+
) {
7697
return this.openai.beta.chat.completions.stream({
7798
model: prompt.model,
7899
messages: prompt.messages as ChatCompletionMessageParam[],
@@ -144,6 +165,7 @@ Common issues are:
144165
});
145166
let { user_id: aiBotUserId } = auth;
146167
let assistant = new Assistant(client, aiBotUserId);
168+
await assistant.loadToolCallCapableModels();
147169

148170
client.on(RoomMemberEvent.Membership, function (_event, member) {
149171
if (member.membership === 'invite' && member.userId === aiBotUserId) {

Diff for: ‎packages/host/tests/acceptance/ai-assistant-test.gts

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { baseRealm } from '@cardstack/runtime-common';
99
import {
1010
APP_BOXEL_ACTIVE_LLM,
1111
DEFAULT_LLM,
12+
DEFAULT_LLM_LIST,
1213
} from '@cardstack/runtime-common/matrix-constants';
1314

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

319-
assert.dom('[data-test-llm-select-item]').exists({ count: 4 });
320+
assert.dom('[data-test-llm-select-item]').exists({
321+
count: DEFAULT_LLM_LIST.length,
322+
});
320323
assert
321324
.dom('[data-test-llm-select-item="google/gemini-pro-1.5"]')
322325
.hasText('google/gemini-pro-1.5');

Diff for: ‎packages/runtime-common/matrix-constants.ts

+2
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ export const DEFAULT_LLM_LIST = [
2020
'google/gemini-pro-1.5',
2121
'openai/gpt-4o',
2222
'openai/gpt-4o-mini',
23+
'deepseek/deepseek-r1',
24+
'deepseek/deepseek-r1-distill-llama-70b',
2325
];

0 commit comments

Comments
 (0)