-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from pufflyai/80-update-sdk-to-use-new-api
update sdk
- Loading branch information
Showing
9 changed files
with
404 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export const SERVICE_URL = "https://api.prompt.studio/api/v1/completion"; | ||
export const SERVICE_URL = "https://api.prompt.studio"; | ||
|
||
export const getServiceUrl = () => { | ||
return process.env.PROMPT_STUDIO_SERVICE_URL || SERVICE_URL; | ||
return `${process.env.PROMPT_STUDIO_SERVICE_BASE_URL || SERVICE_URL}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import axios from "axios"; | ||
import { createCompletion } from "./createCompletion"; | ||
|
||
jest.mock("axios"); | ||
|
||
describe("createCompletion", () => { | ||
const mockAxios = axios as jest.Mocked<typeof axios>; | ||
|
||
const mockResponse = { | ||
datapoint: { | ||
model_output: "Hello, world!", | ||
model_input: "Hello", | ||
model_id: "model_123", | ||
}, | ||
}; | ||
|
||
const mockInput = { | ||
apiKey: "my-api-key", | ||
modelId: "model_123", | ||
prompt: "Hello", | ||
parameters: { name: "John" }, | ||
config: { temperature: 0.5 }, | ||
options: { track: true }, | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it("should make a POST request to the correct URL with the correct data", async () => { | ||
mockAxios.post.mockResolvedValueOnce({ data: mockResponse }); | ||
|
||
const result = await createCompletion(mockInput); | ||
|
||
expect(mockAxios.post).toHaveBeenCalledTimes(1); | ||
expect(mockAxios.post).toHaveBeenCalledWith( | ||
"https://api.prompt.studio/api/v1/completion/buffered", | ||
{ | ||
modelId: "model_123", | ||
prompt: "Hello", | ||
parameters: { name: "John" }, | ||
config: { temperature: 0.5 }, | ||
options: { track: true }, | ||
}, | ||
{ | ||
headers: { | ||
Authorization: "Bearer my-api-key", | ||
"Content-Type": "application/json", | ||
}, | ||
} | ||
); | ||
expect(result).toEqual(mockResponse); | ||
}); | ||
|
||
it("should throw an error if the request fails", async () => { | ||
const mockError = new Error("Request failed"); | ||
mockAxios.post.mockRejectedValueOnce(mockError); | ||
|
||
await expect(createCompletion(mockInput)).rejects.toThrow(mockError); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { createCompletion } from "./createCompletion"; | ||
export { refineCompletion } from "./refineCompletion"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { mapCompletion } from "./mapCompletion"; | ||
import axios from "axios"; | ||
|
||
jest.mock("axios"); | ||
|
||
describe("mapCompletion", () => { | ||
const mockAxios = axios as jest.Mocked<typeof axios>; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should return completions for each chunk of the document", async () => { | ||
const input = { | ||
apiKey: "myApiKey", | ||
modelId: "myModelId", | ||
prompt: "myPrompt", | ||
document: "myDocument", | ||
parameters: { myParam: "myValue" }, | ||
config: { myConfig: "myValue" }, | ||
options: { track: true, cache: false }, | ||
}; | ||
|
||
const mockResponse = { | ||
data: { | ||
completions: [ | ||
{ | ||
datapoints: { | ||
model_output: "output1", | ||
model_input: "input1", | ||
model_id: "id1", | ||
}, | ||
}, | ||
{ | ||
datapoints: { | ||
model_output: "output2", | ||
model_input: "input2", | ||
model_id: "id2", | ||
}, | ||
}, | ||
], | ||
}, | ||
}; | ||
|
||
mockAxios.post.mockResolvedValueOnce(mockResponse); | ||
|
||
const result = await mapCompletion(input); | ||
|
||
expect(mockAxios.post).toHaveBeenCalledTimes(1); | ||
expect(mockAxios.post).toHaveBeenCalledWith( | ||
"https://api.prompt.studio/api/v1/completion/mapped", | ||
{ | ||
prompt: "myPrompt", | ||
document: "myDocument", | ||
modelId: "myModelId", | ||
parameters: { myParam: "myValue" }, | ||
config: { myConfig: "myValue" }, | ||
options: { track: true, cache: false }, | ||
}, | ||
{ | ||
headers: { | ||
Authorization: "Bearer myApiKey", | ||
"Content-Type": "application/json", | ||
}, | ||
} | ||
); | ||
|
||
expect(result).toEqual({ | ||
completions: [ | ||
{ | ||
datapoints: { | ||
model_output: "output1", | ||
model_input: "input1", | ||
model_id: "id1", | ||
}, | ||
}, | ||
{ | ||
datapoints: { | ||
model_output: "output2", | ||
model_input: "input2", | ||
model_id: "id2", | ||
}, | ||
}, | ||
], | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import axios from "axios"; | ||
import { getServiceUrl } from "./constants"; | ||
|
||
interface MapCompletionInput { | ||
apiKey: string; | ||
modelId: string; | ||
prompt: string; | ||
document: string; | ||
parameters?: Record<string, any>; | ||
config?: Record<string, any>; | ||
options?: { | ||
track?: boolean; | ||
cache?: boolean; | ||
}; | ||
} | ||
|
||
interface Completion { | ||
datapoints?: { | ||
model_output: string; | ||
model_input: string; | ||
model_id: string; | ||
}; | ||
} | ||
|
||
interface MapCompletionPayload { | ||
modelId: string; | ||
prompt: string; | ||
document: string; | ||
parameters?: Record<string, any>; | ||
config?: Record<string, any>; | ||
options?: { | ||
track?: boolean; | ||
cache?: boolean; | ||
}; | ||
} | ||
|
||
/** | ||
* Map a prompt over a document of variable length. Return a completion for each chunk. | ||
* | ||
* @param input.document - The document to be processed | ||
* @param input.parameters - Parameters to be passed to the model | ||
* @param input.modelId - Name of the LLM to be used | ||
* | ||
* @returns The completion | ||
*/ | ||
export async function mapCompletion(input: MapCompletionInput): Promise<{ completions: Completion[] }> { | ||
const { modelId, prompt, document, apiKey, config, options, parameters = {} } = input; | ||
|
||
const payload: MapCompletionPayload = { | ||
modelId, | ||
prompt, | ||
document, | ||
parameters, | ||
}; | ||
|
||
const requestConfig = { | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${apiKey}`, | ||
}, | ||
}; | ||
|
||
if (config) { | ||
payload.config = config; | ||
} | ||
|
||
if (options) { | ||
payload.options = options; | ||
} | ||
|
||
const response = await axios.post(`${getServiceUrl()}/api/v1/completion/mapped`, payload, requestConfig); | ||
|
||
return response.data; | ||
} |
Oops, something went wrong.