Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
cfortuner committed Oct 25, 2024
1 parent a694c79 commit 25e5202
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { type ConfigOpts,OpenAIClient } from './openai-client.js';
export { type ConfigOpts, OpenAIClient } from './openai-client.js';
export type {
ModerationParams,
ModerationResponse,
Expand Down
23 changes: 12 additions & 11 deletions src/openai-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ import { type Options as KyOptions } from 'ky';
import { type OpenAI } from 'openai';

import { StreamCompletionChunker } from './streaming.js';
import {
type SpeechParams,
type SpeechResponse,
} from './types.js';
import { type SpeechParams, type SpeechResponse } from './types.js';

export type ConfigOpts = {
/**
Expand All @@ -49,7 +46,9 @@ export type ConfigOpts = {
kyOptions?: KyOptions;
};

export class OpenAIClient implements AIFetchClient, AIChatClient, AIEmbeddingClient, AICompletionClient {
export class OpenAIClient
implements AIFetchClient, AIChatClient, AIEmbeddingClient, AICompletionClient
{
name = 'openai';
api: ReturnType<typeof createApiInstance>;

Expand All @@ -58,9 +57,7 @@ export class OpenAIClient implements AIFetchClient, AIChatClient, AIEmbeddingCli
const apiKey = opts.apiKey || process.env.OPENAI_API_KEY;
const organizationId = opts.organizationId || process.env.OPENAI_ORG_ID;
const prefixUrl =
opts.baseUrl ||
process.env.OPENAI_BASE_URL ||
'https://api.openai.com';
opts.baseUrl || process.env.OPENAI_BASE_URL || 'https://api.openai.com';
if (!apiKey)
throw new Error(
'Missing OpenAI API key. Please provide one in the config or set the OPENAI_API_KEY environment variable.'
Expand All @@ -71,7 +68,7 @@ export class OpenAIClient implements AIFetchClient, AIChatClient, AIEmbeddingCli
headers: {
...opts.kyOptions?.headers,
'User-Agent': 'openai-fetch',
'Authorization': `Bearer ${apiKey}`,
Authorization: `Bearer ${apiKey}`,
'OpenAI-Organization': organizationId,
},
});
Expand All @@ -94,7 +91,9 @@ export class OpenAIClient implements AIFetchClient, AIChatClient, AIEmbeddingCli

/** Create a chat completion and stream back partial progress. */
async streamChatCompletion(
params: ChatStreamParams<OpenAI.ChatCompletionCreateParamsStreaming['model']>,
params: ChatStreamParams<
OpenAI.ChatCompletionCreateParamsStreaming['model']
>,
opts?: AIFetchRequestOpts
): Promise<ChatStreamResponse> {
const response = await this.getApi(opts).post('chat/completions', {
Expand Down Expand Up @@ -122,7 +121,9 @@ export class OpenAIClient implements AIFetchClient, AIChatClient, AIEmbeddingCli

/** Create a completion for a single prompt string and stream back partial progress. */
async streamCompletion(
params: CompletionStreamParams<OpenAI.CompletionCreateParamsStreaming['model']>,
params: CompletionStreamParams<
OpenAI.CompletionCreateParamsStreaming['model']
>,
opts?: AIFetchRequestOpts
): Promise<CompletionStreamResponse> {
const response = await this.getApi(opts).post('completions', {
Expand Down

0 comments on commit 25e5202

Please sign in to comment.