Skip to content

Commit

Permalink
Update openai package imports
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubknejzlik committed Aug 5, 2024
1 parent f74be96 commit d16133a
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/chains/prompt-with-pick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ const sortPromptResults = async <T extends ShapeType>(
run: {
...run,
...pickOpts,
instructions: `Your task is to help to identify best options for given instructions: ${run.instructions ?? ''}${run.additional_instructions ?? ''} ${pickOpts.instructions ?? ''}${pickOpts.additional_instructions ?? ''} # Generated options:\n${results.map((r, i) => `\n##Option '${optionNames[`${i}`]}':\n===\n${JSON.stringify(r)}\n===`).join('\n')}.`,
instructions: `Your task is to help to identify best options for given instructions: ${run.instructions ?? ''}${run.additional_instructions ?? ''} ${pickOpts?.instructions ?? ''}${pickOpts?.additional_instructions ?? ''} # Generated options:\n${results.map((r, i) => `\n##Option '${optionNames[`${i}`]}':\n===\n${JSON.stringify(r)}\n===`).join('\n')}.`,
additional_instructions: `Please sort the options in order from best answer to worst.
Include all options ['${optionNames.join("','")}']. Example {"pickedItems":["X", "Y", "Z"]}`,
Include all options ['${optionNames.join("','")}']. Example {"pickedItems":["X", "Y", "Z"]}`
},
...rest,
responseObject: z.object({ pickedItems: z.array(z.string().length(1)) }),
Expand Down
27 changes: 10 additions & 17 deletions src/completions/completion-with-functions.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
import type { ChatCompletionFunction } from '../function'
import OpenAI from 'openai'
import type { z } from 'zod'
import type { ChatCompletionFunction } from '../function'

import {
ChatCompletionMessageParam,
ChatCompletionTool
} from 'openai/resources'
import zodToJsonSchema from 'zod-to-json-schema'
import {
ChatCompletionCreateParamsBase,
ChatCompletionMessage,
ChatCompletionMessageToolCall
} from 'openai/resources/chat/completions'

type CompletionOpts = Partial<
Omit<ChatCompletionCreateParamsBase, 'functions' | 'tools'>
Omit<OpenAI.ChatCompletionCreateParams, 'functions' | 'tools'>
> & {
client: OpenAI
// options?: Partial<OpenAI.Chat.Completions.ChatCompletionCreateParamsNonStreaming>
instructions: string
prompt?: string
messages?: ChatCompletionMessageParam[]
messages?: OpenAI.ChatCompletionMessageParam[]
}

export type CompletionOptsWithFunctionOpts = CompletionOpts & {
Expand All @@ -30,7 +21,7 @@ export type CompletionOptsWithFunctionOpts = CompletionOpts & {

export const functionToOpenAIChatCompletionTool = <T extends z.ZodRawShape>(
fn: ChatCompletionFunction<T>
): ChatCompletionTool => {
): OpenAI.ChatCompletionTool => {
const params = fn.parameters ? zodToJsonSchema(fn.parameters) : undefined
return {
type: 'function',
Expand All @@ -44,7 +35,7 @@ export const functionToOpenAIChatCompletionTool = <T extends z.ZodRawShape>(

export const completionWithFunctions = async (
opts: CompletionOptsWithFunctionOpts
): Promise<ChatCompletionMessage> => {
): Promise<OpenAI.ChatCompletionMessage> => {
const {
client,
instructions,
Expand All @@ -57,15 +48,15 @@ export const completionWithFunctions = async (
} = opts

// initialize messages
const _messages: ChatCompletionMessageParam[] = messages ?? [
const _messages: OpenAI.ChatCompletionMessageParam[] = messages ?? [
{ role: 'system', content: instructions }
]
if (prompt) {
_messages.push({ role: 'user', content: prompt })
}

const response = await client.chat.completions.create({
model: model,
model: model ?? 'gpt-4o-mini',
messages: _messages,
tools: functions?.map(functionToOpenAIChatCompletionTool),
...rest,
Expand All @@ -74,7 +65,9 @@ export const completionWithFunctions = async (

let message = response?.choices?.[0]?.message

const handleToolCall = async (toolCall: ChatCompletionMessageToolCall) => {
const handleToolCall = async (
toolCall: OpenAI.ChatCompletionMessageToolCall
) => {
try {
const fn = functions?.find((f) => f.name === toolCall.function.name)
if (!fn) {
Expand Down
5 changes: 2 additions & 3 deletions src/completions/completion-with-json.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import type { z } from 'zod'
import type { ChatCompletionFunction } from '../function'

import { ChatCompletionTool } from 'openai/resources'
import zodToJsonSchema from 'zod-to-json-schema'
import {
CompletionOptsWithFunctionOpts,
completionWithFunctions
} from './completion-with-functions'
import { InferedType } from '../chains/prompt-with-retry'
import OpenAI from 'openai'

export type CompletionOptsWithJsonResponse<T extends z.ZodRawShape> =
CompletionOptsWithFunctionOpts & {
Expand All @@ -17,7 +16,7 @@ export type CompletionOptsWithJsonResponse<T extends z.ZodRawShape> =

export const functionToOpenAIChatCompletionTool = <T extends z.ZodRawShape>(
fn: ChatCompletionFunction<T>
): ChatCompletionTool => {
): OpenAI.ChatCompletionTool => {
const params = fn.parameters ? zodToJsonSchema(fn.parameters) : undefined
return {
type: 'function',
Expand Down
5 changes: 2 additions & 3 deletions src/files.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { FileObject } from 'openai/resources'
import { toFile, type Uploadable } from 'openai/uploads'

import OpenAI from 'openai'
Expand All @@ -7,8 +6,8 @@ import { getDefaultOpenAIClient } from './openai-client'
export const handleFileUpload = async (
file: Uploadable,
filename: string | null,
client?:OpenAI,
): Promise<FileObject> => {
client?: OpenAI
): Promise<OpenAI.FileObject> => {
return (client ?? getDefaultOpenAIClient()).files.create({
file: await toFile(file, filename),
purpose: 'assistants'
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ export { promptWithRetry } from './chains/prompt-with-retry'
export { createChatCompletionFunction } from './function'
export { createOpenAIClient, getDefaultOpenAIClient } from './openai-client'

export { Thread, ThreadPromptWithFunctionOpts } from './thread'
export type { Thread, ThreadPromptWithFunctionOpts } from './thread'
20 changes: 10 additions & 10 deletions src/openai-client.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import OpenAI, { ClientOptions } from 'openai';
import OpenAI, { ClientOptions } from 'openai'

export const createOpenAIClient = (opts:ClientOptions={}): OpenAI => {
return new OpenAI(opts);
};
export const createOpenAIClient = (opts: ClientOptions = {}): OpenAI => {
return new OpenAI(opts)
}

let _defaultOpenAIClient: OpenAI = undefined
let _defaultOpenAIClient: OpenAI | undefined = undefined
export const getDefaultOpenAIClient = (): OpenAI => {
if(!_defaultOpenAIClient) {
_defaultOpenAIClient=createOpenAIClient()
}
return _defaultOpenAIClient
}
if (!_defaultOpenAIClient) {
_defaultOpenAIClient = createOpenAIClient()
}
return _defaultOpenAIClient
}
7 changes: 4 additions & 3 deletions src/thread.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type OpenAI from 'openai'
import type { AssistantStream } from 'openai/lib/AssistantStream'
import type { FileObject } from 'openai/resources'
import pRetry from 'p-retry'
import type { z } from 'zod'
import zodToJsonSchema from 'zod-to-json-schema'
Expand All @@ -9,9 +8,11 @@ import {
type ChatCompletionFunction,
functionToOpenAIAssistantTool
} from './function'
import { AssistantStreamEvent } from 'openai/resources/beta/assistants'
// import { AssistantStreamEvent } from 'openai/resources/beta/assistants'
import { getDefaultOpenAIClient } from './openai-client'

type AssistantStreamEvent = OpenAI.Beta.Assistants.AssistantStreamEvent

interface ThreadOptions {
threadId?: string
client?: OpenAI
Expand Down Expand Up @@ -330,7 +331,7 @@ export class Thread {
}
}

async attachFiles(files: FileObject[]): Promise<void> {
async attachFiles(files: OpenAI.FileObject[]): Promise<void> {
const thread = await this.getThread()
const vector_store_ids =
thread.tool_resources?.file_search?.vector_store_ids ?? []
Expand Down

0 comments on commit d16133a

Please sign in to comment.