Skip to content

Commit 39af1b1

Browse files
committed
Fix tsq schema
1 parent 2b1a2dc commit 39af1b1

File tree

2 files changed

+122
-4
lines changed

2 files changed

+122
-4
lines changed

plugins/qualifire/globals.ts

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,25 @@ import { post } from '../utils';
33
// export const BASE_URL = 'https://proxy.qualifire.ai/api/evaluation/evaluate';
44
export const BASE_URL = 'http://localhost:8080/api/evaluation/evaluate';
55

6+
interface AvailableTool {
7+
name: string;
8+
description: string;
9+
parameters: object;
10+
}
11+
12+
interface ToolCall {
13+
id: string;
14+
name: string;
15+
arguments: any;
16+
}
17+
18+
interface Message {
19+
role: string;
20+
content: string;
21+
tool_call_id?: string;
22+
tool_calls?: ToolCall[];
23+
}
24+
625
export const postQualifire = async (
726
body: any,
827
qualifireApiKey?: string,
@@ -30,3 +49,99 @@ export const postQualifire = async (
3049

3150
return { error, verdict, data };
3251
};
52+
53+
export const parseAvailableTools = (
54+
request: any
55+
): AvailableTool[] | undefined => {
56+
const tools = request?.json?.tools ?? [];
57+
const functionTools = tools.filter((tool: any) => tool.type === 'function');
58+
59+
if (functionTools.length === 0) {
60+
return undefined;
61+
}
62+
63+
return functionTools.map((tool: any) => ({
64+
name: tool.function.name,
65+
description: tool.function.description,
66+
parameters: tool.function.parameters,
67+
}));
68+
};
69+
70+
const convertContent = (content: any) => {
71+
if (!content) {
72+
return '';
73+
} else if (typeof content === 'string') {
74+
return content;
75+
} else if (!Array.isArray(content)) {
76+
return JSON.stringify(content); // unexpected format, pass as raw
77+
}
78+
79+
return content
80+
.map((part: any) => {
81+
if (part.type === 'text') {
82+
return part.text;
83+
}
84+
return '\n' + JSON.stringify(part) + '\n';
85+
})
86+
.join('');
87+
};
88+
89+
const convertToolCalls = (toolCalls: any) => {
90+
if (!toolCalls || toolCalls.length === 0) {
91+
return undefined;
92+
}
93+
94+
toolCalls = toolCalls.filter((toolCall: any) => toolCall.type === 'function');
95+
if (toolCalls.length === 0) {
96+
return undefined;
97+
}
98+
99+
return toolCalls.map((toolCall: any) => ({
100+
id: toolCall.id,
101+
name: toolCall.function.name,
102+
arguments: JSON.parse(toolCall.function?.arguments ?? '{}'),
103+
}));
104+
};
105+
106+
export const convertToMessages = (
107+
request: any,
108+
response: any,
109+
ignoreRequestHistory: boolean = true
110+
): Message[] => {
111+
let messages = request.json.messages;
112+
113+
if (ignoreRequestHistory) {
114+
messages = [messages[messages.length - 1]];
115+
}
116+
117+
// convert request
118+
const requestMessages = messages.map((message: any) => {
119+
const role = message.role;
120+
const content = convertContent(message.content);
121+
122+
return {
123+
role: role,
124+
content: content,
125+
tool_calls: message.tool_calls ?? undefined,
126+
tool_call_id: message.tool_call_id ?? undefined,
127+
};
128+
});
129+
130+
// convert response if given
131+
if ((response?.json?.choices || []).length === 0) {
132+
return requestMessages;
133+
}
134+
if (!response.json.choices[0].message) {
135+
return requestMessages;
136+
}
137+
138+
const responseMessage = response.json.choices[0].message;
139+
140+
const convertedResponseMessage = {
141+
role: responseMessage.role,
142+
content: convertContent(responseMessage.content),
143+
tool_calls: convertToolCalls(responseMessage.tool_calls),
144+
};
145+
146+
return [...requestMessages, convertedResponseMessage];
147+
};

plugins/qualifire/toolSelectionQuality.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import {
44
PluginHandler,
55
PluginParameters,
66
} from '../types';
7-
import { postQualifire } from './globals';
7+
import {
8+
convertToMessages,
9+
parseAvailableTools,
10+
postQualifire,
11+
} from './globals';
812

913
export const handler: PluginHandler = async (
1014
context: PluginContext,
@@ -27,9 +31,8 @@ export const handler: PluginHandler = async (
2731
}
2832

2933
const evaluationBody: any = {
30-
// TODO build correct body
31-
input: context.request.text,
32-
output: context.response.text,
34+
messages: convertToMessages(context.request, context.response),
35+
available_tools: parseAvailableTools(context.request),
3336
tool_selection_quality_check: true,
3437
};
3538

0 commit comments

Comments
 (0)