Skip to content

Commit e9cb515

Browse files
committed
tools and fix types
1 parent ef39630 commit e9cb515

File tree

7 files changed

+137
-50
lines changed

7 files changed

+137
-50
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
],
2222
"scripts": {
2323
"build": "run-p build:*",
24-
"build:main": "tsc -p tsconfig.json",
25-
"build:module": "tsc -p tsconfig.module.json",
24+
"build:main": "tsc -p tsconfig.json && npx openapicmd typegen https://api.promptfoundry.ai/sdk/openapi > ./build/main/types/openapi.d.ts",
25+
"build:module": "tsc -p tsconfig.module.json && npx openapicmd typegen https://api.promptfoundry.ai/sdk/openapi > ./build/module/types/openapi.d.ts",
2626
"fix": "run-s fix:*",
2727
"lint": "eslint .",
2828
"prettier": "npm run lint -- --fix",

src/__tests__/Client.test.ts

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import APIClient from '../ApiClient'
22
import Client from '../Client'
3+
import { PromptConfiguration, PromptMessageRoleEnum } from '../types'
34

45
describe('client', () => {
56
describe('init client', () => {
@@ -61,28 +62,30 @@ describe('client', () => {
6162
it('should replace variables', async () => {
6263
const client = new Client({ apiKey: '123' })
6364

64-
const value = {
65+
const value: PromptConfiguration = {
6566
promptId: 'HELLO',
6667
promptParameters: {
6768
frequencyPenalty: 0,
6869
modelName: 'gpt-3.5-turbo',
6970
presencePenalty: 0,
70-
responseFormat: {
71-
type: 'text'
72-
},
71+
responseFormat: 'text',
7372
temperature: 0.7,
74-
topP: 1
73+
topP: 1,
74+
seed: null,
75+
maxTokens: null,
76+
toolChoice: 'auto'
7577
},
7678
promptMessages: [
7779
{
7880
content: 'Hello, world!',
79-
role: 'user'
81+
role: PromptMessageRoleEnum.USER
8082
},
8183
{
8284
content: 'Hi there {{name}}!',
83-
role: 'assistant'
85+
role: PromptMessageRoleEnum.ASSISTANT
8486
}
85-
]
87+
],
88+
promptTools: []
8689
}
8790

8891
const apiClient: APIClient = {
@@ -102,28 +105,30 @@ describe('client', () => {
102105
it('should throw error if missing variables', async () => {
103106
const client = new Client({ apiKey: '123' })
104107

105-
const value = {
108+
const value: PromptConfiguration = {
106109
promptId: 'HELLO',
107110
promptParameters: {
108111
frequencyPenalty: 0,
109112
modelName: 'gpt-3.5-turbo',
110113
presencePenalty: 0,
111-
responseFormat: {
112-
type: 'text'
113-
},
114+
responseFormat: 'text',
114115
temperature: 0.7,
115-
topP: 1
116+
topP: 1,
117+
seed: null,
118+
maxTokens: null,
119+
toolChoice: 'auto'
116120
},
117121
promptMessages: [
118122
{
119123
content: 'Hello, world!',
120-
role: 'user'
124+
role: PromptMessageRoleEnum.USER
121125
},
122126
{
123127
content: 'Hi there {{name}}!',
124-
role: 'assistant'
128+
role: PromptMessageRoleEnum.ASSISTANT
125129
}
126-
]
130+
],
131+
promptTools: []
127132
}
128133

129134
const apiClient: APIClient = {
@@ -143,28 +148,30 @@ describe('client', () => {
143148
it('returnsmapped prompt', async () => {
144149
const client = new Client({ apiKey: '123' })
145150

146-
const value = {
151+
const value: PromptConfiguration = {
147152
promptId: 'HELLO',
148153
promptParameters: {
149154
frequencyPenalty: 0,
150155
modelName: 'gpt-3.5-turbo',
151156
presencePenalty: 0,
152-
responseFormat: {
153-
type: 'text'
154-
},
157+
responseFormat: 'text',
155158
temperature: 0.7,
156-
topP: 1
159+
topP: 1,
160+
seed: null,
161+
maxTokens: null,
162+
toolChoice: 'auto'
157163
},
158164
promptMessages: [
159165
{
160166
content: 'Hello, world!',
161-
role: 'user'
167+
role: PromptMessageRoleEnum.USER
162168
},
163169
{
164170
content: 'Hi there!',
165-
role: 'assistant'
171+
role: PromptMessageRoleEnum.ASSISTANT
166172
}
167-
]
173+
],
174+
promptTools: []
168175
}
169176

170177
const apiClient: APIClient = {
@@ -184,28 +191,30 @@ describe('client', () => {
184191
it('should replace variables', async () => {
185192
const client = new Client({ apiKey: '123' })
186193

187-
const value = {
194+
const value: PromptConfiguration = {
188195
promptId: 'HELLO',
189196
promptParameters: {
190197
frequencyPenalty: 0,
191198
modelName: 'gpt-3.5-turbo',
192199
presencePenalty: 0,
193-
responseFormat: {
194-
type: 'text'
195-
},
200+
responseFormat: 'text',
196201
temperature: 0.7,
197-
topP: 1
202+
topP: 1,
203+
seed: null,
204+
maxTokens: null,
205+
toolChoice: 'auto'
198206
},
199207
promptMessages: [
200208
{
201209
content: 'Hello, world!',
202-
role: 'user'
210+
role: PromptMessageRoleEnum.USER
203211
},
204212
{
205213
content: 'Hi there {{name}}!',
206-
role: 'assistant'
214+
role: PromptMessageRoleEnum.ASSISTANT
207215
}
208-
]
216+
],
217+
promptTools: []
209218
}
210219

211220
const apiClient: APIClient = {
@@ -225,28 +234,30 @@ describe('client', () => {
225234
it('should throw error if missing variables', async () => {
226235
const client = new Client({ apiKey: '123' })
227236

228-
const value = {
237+
const value: PromptConfiguration = {
229238
promptId: 'HELLO',
230239
promptParameters: {
231240
frequencyPenalty: 0,
232241
modelName: 'gpt-3.5-turbo',
233242
presencePenalty: 0,
234-
responseFormat: {
235-
type: 'text'
236-
},
243+
responseFormat: 'text',
237244
temperature: 0.7,
238-
topP: 1
245+
topP: 1,
246+
seed: null,
247+
maxTokens: null,
248+
toolChoice: 'auto'
239249
},
240250
promptMessages: [
241251
{
242252
content: 'Hello, world!',
243-
role: 'user'
253+
role: PromptMessageRoleEnum.USER
244254
},
245255
{
246256
content: 'Hi there {{name}}!',
247-
role: 'assistant'
257+
role: PromptMessageRoleEnum.ASSISTANT
248258
}
249-
]
259+
],
260+
promptTools: []
250261
}
251262

252263
const apiClient: APIClient = {

src/__tests__/__snapshots__/Client.test.ts.snap

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
exports[`client getOpenAiPrompt returnsmapped prompt 1`] = `
44
{
55
"frequency_penalty": 0,
6-
"max_tokens": undefined,
6+
"max_tokens": null,
77
"messages": [
88
{
99
"content": "Hello, world!",
@@ -19,8 +19,10 @@ exports[`client getOpenAiPrompt returnsmapped prompt 1`] = `
1919
"response_format": {
2020
"type": "text",
2121
},
22-
"seed": undefined,
22+
"seed": null,
2323
"temperature": 0.7,
24+
"tool_choice": "auto",
25+
"tools": [],
2426
"top_p": 1,
2527
}
2628
`;

src/helpers/__tests__/template.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ describe('template helpers', () => {
161161
responseFormat: 'json',
162162
seed: 0
163163
},
164-
164+
promptTools: [],
165165
promptMessages: [{ role: PromptMessageRoleEnum.USER, content: 'Hello, {{name}}!' }]
166166
}
167167
const variables = { name: 'Alice' }
@@ -185,6 +185,7 @@ describe('template helpers', () => {
185185
responseFormat: 'json',
186186
seed: 0
187187
},
188+
promptTools: [],
188189
promptMessages: [{ role: PromptMessageRoleEnum.USER, content: 'Hello!' }]
189190
}
190191
const variables = {}
@@ -209,6 +210,7 @@ describe('template helpers', () => {
209210
responseFormat: 'json',
210211
seed: 0
211212
},
213+
promptTools: [],
212214
promptMessages: [{ role: PromptMessageRoleEnum.USER, content: 'Hello, {{name}}!' }]
213215
}
214216
const variables = { name: 'Alice' }
@@ -229,6 +231,7 @@ describe('template helpers', () => {
229231
responseFormat: 'json',
230232
seed: 0
231233
},
234+
promptTools: [],
232235
promptMessages: [{ role: PromptMessageRoleEnum.USER, content: 'Hello, {{name}}!' }]
233236
}
234237
const variables = {}

src/helpers/openAi.ts

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
import { ChatCompletionCreateParamsNonStreaming, ChatCompletionMessageParam, ChatCompletionRole } from 'openai/resources'
1+
import {
2+
ChatCompletionCreateParamsNonStreaming,
3+
ChatCompletionMessageParam,
4+
ChatCompletionRole,
5+
ChatCompletionTool,
6+
ChatCompletionToolChoiceOption
7+
} from 'openai/resources'
28

3-
import { PromptConfiguration, PromptMessage } from '../types'
9+
import { PromptConfiguration, PromptMessage, PromptTool } from '../types'
410

511
export const mapMessagesToOpenAI = (promptMessages: PromptMessage[]): ChatCompletionMessageParam[] => {
612
return promptMessages.map((message) => ({
@@ -9,8 +15,42 @@ export const mapMessagesToOpenAI = (promptMessages: PromptMessage[]): ChatComple
915
})) as ChatCompletionMessageParam[]
1016
}
1117

12-
export const mapPromptToOpenAIConfig = (promptInstance: PromptConfiguration): ChatCompletionCreateParamsNonStreaming => {
13-
const { promptMessages, promptParameters } = promptInstance
18+
const mapToolChoiceToOpenAI = (tools: PromptTool[], toolChoice?: string | null): ChatCompletionToolChoiceOption => {
19+
if (toolChoice === 'auto' || (!toolChoice && tools.length !== 0)) {
20+
return 'auto'
21+
}
22+
if (toolChoice === 'none' || tools.length === 0) {
23+
return 'none'
24+
}
25+
26+
if (toolChoice) {
27+
const tool = tools.find((t) => t.name === toolChoice)
28+
if (tool) {
29+
return {
30+
type: 'function',
31+
function: {
32+
name: tool.name
33+
}
34+
}
35+
}
36+
}
37+
38+
return 'none'
39+
}
40+
41+
const mapToolToOpenAi = (tool: PromptTool): ChatCompletionTool => {
42+
return {
43+
type: 'function',
44+
function: {
45+
name: tool.name,
46+
description: tool.description,
47+
parameters: tool.parameters
48+
}
49+
}
50+
}
51+
52+
export const mapPromptToOpenAIConfig = (promptConfig: PromptConfiguration): ChatCompletionCreateParamsNonStreaming => {
53+
const { promptMessages, promptParameters, promptTools } = promptConfig
1454

1555
const messages = mapMessagesToOpenAI(promptMessages)
1656

@@ -23,8 +63,10 @@ export const mapPromptToOpenAIConfig = (promptInstance: PromptConfiguration): Ch
2363
seed: promptParameters.seed,
2464
presence_penalty: promptParameters.presencePenalty,
2565
frequency_penalty: promptParameters.frequencyPenalty,
66+
tool_choice: mapToolChoiceToOpenAI(promptTools, promptParameters.toolChoice),
2667
response_format: {
2768
type: promptParameters.responseFormat === 'json' ? 'json_object' : 'text'
28-
}
69+
},
70+
tools: promptTools.map((tool) => mapToolToOpenAi(tool))
2971
}
3072
}

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Components } from './openapi'
33
export type PromptConfiguration = Components.Schemas.PromptConfiguration
44
export type PromptMessage = Components.Schemas.PromptMessage
55
export type PromptMessageRole = PromptMessage['role']
6+
export type PromptTool = Components.Schemas.Tool
67
export enum PromptMessageRoleEnum {
78
SYSTEM = 'SYSTEM',
89
ASSISTANT = 'ASSISTANT',

src/types/openapi.d.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,14 @@ declare namespace Components {
6262
* 97946543
6363
*/
6464
seed: number | null;
65+
/**
66+
* example:
67+
* checkWeather
68+
*/
69+
toolChoice?: string | null;
6570
};
6671
promptMessages: /* The initial messages to be included with your call to the LLM API. */ PromptMessage[];
72+
promptTools: Tool[];
6773
}
6874
/**
6975
* The initial messages to be included with your call to the LLM API.
@@ -80,6 +86,28 @@ declare namespace Components {
8086
*/
8187
role: "USER" | "ASSISTANT" | "SYSTEM" | "TOOL";
8288
}
89+
export interface Tool {
90+
/**
91+
* example:
92+
* TOOL_1
93+
*/
94+
toolId?: string;
95+
/**
96+
* The name of the tool to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64.
97+
*/
98+
name: string; // ^[a-zA-Z0-9_-]{1,64}$
99+
/**
100+
* A description of what the tool does, used by the model to choose when and how to call the tool.
101+
*/
102+
description: string;
103+
parameters: /* The parameters the functions accepts, described as a JSON Schema object. This schema is designed to match the TypeScript Record<string, unknown>, allowing for any properties with values of any type. */ ToolParameters;
104+
}
105+
/**
106+
* The parameters the functions accepts, described as a JSON Schema object. This schema is designed to match the TypeScript Record<string, unknown>, allowing for any properties with values of any type.
107+
*/
108+
export interface ToolParameters {
109+
[name: string]: any;
110+
}
83111
}
84112
}
85113
declare namespace Paths {

0 commit comments

Comments
 (0)