From ce1ef9dd0f3ca47a141ce1bf1e23c8c11fd63d30 Mon Sep 17 00:00:00 2001 From: stainless-bot Date: Wed, 7 Aug 2024 18:12:05 +0000 Subject: [PATCH] feat(api): update via SDK Studio --- .stats.yml | 4 +- api.md | 10 + src/index.ts | 5 + src/resources/completion.ts | 320 +++++++++++++++++++++++++ src/resources/index.ts | 1 + tests/api-resources/completion.test.ts | 95 ++++++++ 6 files changed, 433 insertions(+), 2 deletions(-) create mode 100644 src/resources/completion.ts create mode 100644 tests/api-resources/completion.test.ts diff --git a/.stats.yml b/.stats.yml index 55288c9..600547a 100644 --- a/.stats.yml +++ b/.stats.yml @@ -1,2 +1,2 @@ -configured_endpoints: 21 -openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/prompt-foundry%2Fprompt-foundry-sdk-89af1acee06cb9beab0634c1d49d2d7bd6ee4a8a65ac12463ffa1275e8b85a0c.yml +configured_endpoints: 22 +openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/prompt-foundry%2Fprompt-foundry-sdk-969bf55b04cabb6e8830e65b28eaab5803568e209120cd75d331ea4626772544.yml diff --git a/api.md b/api.md index a35ad6a..0fe79d7 100644 --- a/api.md +++ b/api.md @@ -1,3 +1,13 @@ +# Completion + +Types: + +- CompletionCreateResponse + +Methods: + +- client.completion.create(id, { ...params }) -> CompletionCreateResponse + # Prompts Types: diff --git a/src/index.ts b/src/index.ts index 7fdc84b..a92a217 100644 --- a/src/index.ts +++ b/src/index.ts @@ -125,6 +125,7 @@ export class PromptFoundry extends Core.APIClient { this.apiKey = apiKey; } + completion: API.Completion = new API.Completion(this); prompts: API.Prompts = new API.Prompts(this); tools: API.Tools = new API.Tools(this); evaluationAssertions: API.EvaluationAssertions = new API.EvaluationAssertions(this); @@ -188,6 +189,10 @@ export import fileFromPath = Uploads.fileFromPath; export namespace PromptFoundry { export import RequestOptions = Core.RequestOptions; + export import Completion = API.Completion; + export import CompletionCreateResponse = API.CompletionCreateResponse; + export import CompletionCreateParams = API.CompletionCreateParams; + export import Prompts = API.Prompts; export import ModelParameters = API.ModelParameters; export import PromptConfiguration = API.PromptConfiguration; diff --git a/src/resources/completion.ts b/src/resources/completion.ts new file mode 100644 index 0000000..75e2e49 --- /dev/null +++ b/src/resources/completion.ts @@ -0,0 +1,320 @@ +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +import { APIResource } from '../resource'; +import { isRequestOptions } from '../core'; +import * as Core from '../core'; +import * as CompletionAPI from './completion'; + +export class Completion extends APIResource { + /** + * Initiates a completion request to the configured LLM provider using specified + * parameters and provided variables. This endpoint abstracts the integration with + * different model providers, enabling seamless switching between models while + * maintaining a consistent data model for your application. + */ + create( + id: string, + body?: CompletionCreateParams, + options?: Core.RequestOptions, + ): Core.APIPromise; + create(id: string, options?: Core.RequestOptions): Core.APIPromise; + create( + id: string, + body: CompletionCreateParams | Core.RequestOptions = {}, + options?: Core.RequestOptions, + ): Core.APIPromise { + if (isRequestOptions(body)) { + return this.create(id, {}, body); + } + return this._client.post(`/sdk/v1/prompts/${id}/completion`, { body, ...options }); + } +} + +export interface CompletionCreateResponse { + /** + * The completion message generated by the model. + */ + message: CompletionCreateResponse.Message; + + stats: CompletionCreateResponse.Stats; +} + +export namespace CompletionCreateResponse { + /** + * The completion message generated by the model. + */ + export interface Message { + content: Array< + | Message.TextContentBlockSchema + | Message.ImageBase64ContentBlock + | Message.ToolCallContentBlock + | Message.ToolResultContentBlock + >; + + role: 'assistant' | 'system' | 'tool' | 'user'; + } + + export namespace Message { + export interface TextContentBlockSchema { + text: string; + + type: 'TEXT'; + } + + export interface ImageBase64ContentBlock { + imageBase64: string; + + mediaType: string; + + type: 'IMAGE_BASE64'; + } + + export interface ToolCallContentBlock { + toolCall: ToolCallContentBlock.ToolCall; + + type: 'TOOL_CALL'; + } + + export namespace ToolCallContentBlock { + export interface ToolCall { + function: ToolCall.Function; + + /** + * TOOL_CALL_1 + */ + toolCallId: string; + + /** + * The type of the tool. Currently, only `function` is supported. + */ + type: 'function'; + } + + export namespace ToolCall { + export interface Function { + /** + * The arguments to call the function with, as generated by the model in JSON + * format. Note that the model does not always generate valid JSON, and may + * hallucinate parameters not defined by your function schema. Validate the + * arguments in your code before calling your function. + */ + arguments: string; + + /** + * The name of the function to call. + */ + name: string; + } + } + } + + export interface ToolResultContentBlock { + result: string; + + toolCallId: string; + + type: 'TOOL_RESULT'; + } + } + + export interface Stats { + /** + * The cost of generating the completion. + */ + cost: number; + + /** + * The number of tokens in the input prompt. + */ + inputTokenCount: number; + + /** + * The time in milliseconds it took to generate the completion. + */ + latency: number; + + /** + * The number of tokens in the output completion. + */ + outputTokenCount: number; + } +} + +export interface CompletionCreateParams { + /** + * Appended the the end of the configured prompt messages before running the + * prompt. + */ + appendMessages?: Array; + + /** + * Replaces the configured prompt messages when running the prompt. + */ + overrideMessages?: Array; + + /** + * A unique identifier representing your end-user, which can help monitor and + * detect abuse. + */ + user?: string; + + /** + * The template variables added to the prompt when executing the prompt. + */ + variables?: Record; +} + +export namespace CompletionCreateParams { + export interface AppendMessage { + content: Array< + | AppendMessage.TextContentBlockSchema + | AppendMessage.ImageBase64ContentBlock + | AppendMessage.ToolCallContentBlock + | AppendMessage.ToolResultContentBlock + >; + + role: 'assistant' | 'system' | 'tool' | 'user'; + } + + export namespace AppendMessage { + export interface TextContentBlockSchema { + text: string; + + type: 'TEXT'; + } + + export interface ImageBase64ContentBlock { + imageBase64: string; + + mediaType: string; + + type: 'IMAGE_BASE64'; + } + + export interface ToolCallContentBlock { + toolCall: ToolCallContentBlock.ToolCall; + + type: 'TOOL_CALL'; + } + + export namespace ToolCallContentBlock { + export interface ToolCall { + function: ToolCall.Function; + + /** + * TOOL_CALL_1 + */ + toolCallId: string; + + /** + * The type of the tool. Currently, only `function` is supported. + */ + type: 'function'; + } + + export namespace ToolCall { + export interface Function { + /** + * The arguments to call the function with, as generated by the model in JSON + * format. Note that the model does not always generate valid JSON, and may + * hallucinate parameters not defined by your function schema. Validate the + * arguments in your code before calling your function. + */ + arguments: string; + + /** + * The name of the function to call. + */ + name: string; + } + } + } + + export interface ToolResultContentBlock { + result: string; + + toolCallId: string; + + type: 'TOOL_RESULT'; + } + } + + export interface OverrideMessage { + content: Array< + | OverrideMessage.TextContentBlockSchema + | OverrideMessage.ImageBase64ContentBlock + | OverrideMessage.ToolCallContentBlock + | OverrideMessage.ToolResultContentBlock + >; + + role: 'assistant' | 'system' | 'tool' | 'user'; + } + + export namespace OverrideMessage { + export interface TextContentBlockSchema { + text: string; + + type: 'TEXT'; + } + + export interface ImageBase64ContentBlock { + imageBase64: string; + + mediaType: string; + + type: 'IMAGE_BASE64'; + } + + export interface ToolCallContentBlock { + toolCall: ToolCallContentBlock.ToolCall; + + type: 'TOOL_CALL'; + } + + export namespace ToolCallContentBlock { + export interface ToolCall { + function: ToolCall.Function; + + /** + * TOOL_CALL_1 + */ + toolCallId: string; + + /** + * The type of the tool. Currently, only `function` is supported. + */ + type: 'function'; + } + + export namespace ToolCall { + export interface Function { + /** + * The arguments to call the function with, as generated by the model in JSON + * format. Note that the model does not always generate valid JSON, and may + * hallucinate parameters not defined by your function schema. Validate the + * arguments in your code before calling your function. + */ + arguments: string; + + /** + * The name of the function to call. + */ + name: string; + } + } + } + + export interface ToolResultContentBlock { + result: string; + + toolCallId: string; + + type: 'TOOL_RESULT'; + } + } +} + +export namespace Completion { + export import CompletionCreateResponse = CompletionAPI.CompletionCreateResponse; + export import CompletionCreateParams = CompletionAPI.CompletionCreateParams; +} diff --git a/src/resources/index.ts b/src/resources/index.ts index 29b0381..db2563c 100644 --- a/src/resources/index.ts +++ b/src/resources/index.ts @@ -1,5 +1,6 @@ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. +export { CompletionCreateResponse, CompletionCreateParams, Completion } from './completion'; export { Evaluation, EvaluationListResponse, diff --git a/tests/api-resources/completion.test.ts b/tests/api-resources/completion.test.ts new file mode 100644 index 0000000..189e966 --- /dev/null +++ b/tests/api-resources/completion.test.ts @@ -0,0 +1,95 @@ +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +import PromptFoundry from '@prompt-foundry/typescript-sdk'; +import { Response } from 'node-fetch'; + +const client = new PromptFoundry({ + apiKey: 'My API Key', + baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010', +}); + +describe('resource completion', () => { + test('create', async () => { + const responsePromise = client.completion.create('1212121'); + const rawResponse = await responsePromise.asResponse(); + expect(rawResponse).toBeInstanceOf(Response); + const response = await responsePromise; + expect(response).not.toBeInstanceOf(Response); + const dataAndResponse = await responsePromise.withResponse(); + expect(dataAndResponse.data).toBe(response); + expect(dataAndResponse.response).toBe(rawResponse); + }); + + test('create: request options instead of params are passed correctly', async () => { + // ensure the request options are being passed correctly by passing an invalid HTTP method in order to cause an error + await expect(client.completion.create('1212121', { path: '/_stainless_unknown_path' })).rejects.toThrow( + PromptFoundry.NotFoundError, + ); + }); + + test('create: request options and params are passed correctly', async () => { + // ensure the request options are being passed correctly by passing an invalid HTTP method in order to cause an error + await expect( + client.completion.create( + '1212121', + { + appendMessages: [ + { + content: [ + { type: 'TEXT', text: 'text' }, + { type: 'TEXT', text: 'text' }, + { type: 'TEXT', text: 'text' }, + ], + role: 'assistant', + }, + { + content: [ + { type: 'TEXT', text: 'text' }, + { type: 'TEXT', text: 'text' }, + { type: 'TEXT', text: 'text' }, + ], + role: 'assistant', + }, + { + content: [ + { type: 'TEXT', text: 'text' }, + { type: 'TEXT', text: 'text' }, + { type: 'TEXT', text: 'text' }, + ], + role: 'assistant', + }, + ], + overrideMessages: [ + { + content: [ + { type: 'TEXT', text: 'text' }, + { type: 'TEXT', text: 'text' }, + { type: 'TEXT', text: 'text' }, + ], + role: 'assistant', + }, + { + content: [ + { type: 'TEXT', text: 'text' }, + { type: 'TEXT', text: 'text' }, + { type: 'TEXT', text: 'text' }, + ], + role: 'assistant', + }, + { + content: [ + { type: 'TEXT', text: 'text' }, + { type: 'TEXT', text: 'text' }, + { type: 'TEXT', text: 'text' }, + ], + role: 'assistant', + }, + ], + user: 'user', + variables: { foo: 'string' }, + }, + { path: '/_stainless_unknown_path' }, + ), + ).rejects.toThrow(PromptFoundry.NotFoundError); + }); +});