-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move OpenAI client into a separate package.
- Loading branch information
Showing
24 changed files
with
336 additions
and
380 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 |
---|---|---|
|
@@ -9,5 +9,4 @@ Thumbs.db | |
TODO.md | ||
TODO.later.md | ||
|
||
packages/core/dist | ||
packages/react/dist | ||
packages/*/dist |
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
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,30 +1,50 @@ | ||
{ | ||
"author": "Markus Ecker", | ||
"license": "MIT", | ||
"name": "@beakjs/core", | ||
"version": "0.0.5", | ||
"description": "BeakJS core library", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"scripts": { | ||
"build": "tsc", | ||
"prepublishOnly": "yarn build" | ||
"prepublishOnly": "yarn build", | ||
"clean": "rm -rf dist", | ||
"test": "jest" | ||
}, | ||
"files": [ | ||
"dist/**/*" | ||
], | ||
"dependencies": { | ||
"@beakjs/openai": "0.0.5", | ||
"eventemitter3": "^5.0.1", | ||
"jsonrepair": "^3.2.4", | ||
"uuid": "^9.0.1" | ||
}, | ||
"devDependencies": { | ||
"@types/uuid": "^9.0.6", | ||
"typescript": "^5.0.4" | ||
"typescript": "^5.0.4", | ||
"@types/jest": "^29.5.1", | ||
"jest": "^29.5.0", | ||
"ts-jest": "^29.1.0" | ||
}, | ||
"peerDependencies": {}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/mme/beakjs.git" | ||
}, | ||
"author": "Markus Ecker", | ||
"license": "MIT" | ||
"jest": { | ||
"testTimeout": 60000, | ||
"transform": { | ||
"^.+\\.(ts|tsx)$": "ts-jest" | ||
}, | ||
"testEnvironment": "node", | ||
"moduleFileExtensions": [ | ||
"ts", | ||
"tsx", | ||
"js" | ||
], | ||
"testPathIgnorePatterns": [ | ||
"/node_modules/" | ||
] | ||
} | ||
} |
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,4 +1,4 @@ | ||
export { BeakCore } from "./beak"; | ||
export type { FunctionDefinition } from "./types"; | ||
export { Message, DebugLogger } from "./types"; | ||
export type { OpenAIModel } from "./openai"; | ||
export type { OpenAIModel } from "@beakjs/openai"; |
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,119 @@ | ||
import { | ||
OpenAI, | ||
FetchChatCompletionParams, | ||
OpenAIFunction, | ||
OpenAIMessage, | ||
} from "@beakjs/openai"; | ||
import { | ||
FunctionCall, | ||
FunctionDefinition, | ||
LLMAdapter, | ||
LLMEvent, | ||
Message, | ||
QueryChatCompletionParams, | ||
} from "./types"; | ||
|
||
export class OpenAIAdapter implements LLMAdapter { | ||
constructor(private openai: OpenAI) {} | ||
|
||
countTokens(message: Message): number { | ||
return this.openai.countTokens(message); | ||
} | ||
|
||
async queryChatCompletion(params: QueryChatCompletionParams): Promise<void> { | ||
const openAIParams: FetchChatCompletionParams = { | ||
messages: params.messages.map(messageToOpenAI), | ||
functions: functionsToOpenAIFormat(params.functions), | ||
maxTokens: params.maxTokens, | ||
temperature: params.temperature, | ||
}; | ||
return await this.openai.queryChatCompletion(openAIParams); | ||
} | ||
|
||
on(event: LLMEvent, listener: (...args: any[]) => void): this { | ||
this.openai.on(event, listener); | ||
return this; | ||
} | ||
|
||
off(event: LLMEvent, listener?: (...args: any[]) => void): this { | ||
this.openai.off(event, listener); | ||
return this; | ||
} | ||
} | ||
|
||
function messageToOpenAI(message: Message): OpenAIMessage { | ||
const content = message.content || ""; | ||
if (message.role === "system") { | ||
return { role: message.role, content }; | ||
} else if (message.role === "function") { | ||
return { | ||
role: message.role, | ||
content, | ||
name: message.name, | ||
}; | ||
} else { | ||
let functionCall = functionCallToOpenAI(message.functionCall); | ||
|
||
return { | ||
role: message.role, | ||
content, | ||
...(functionCall !== undefined && { function_call: functionCall }), | ||
}; | ||
} | ||
} | ||
|
||
function functionsToOpenAIFormat( | ||
functions?: FunctionDefinition[] | ||
): OpenAIFunction[] | undefined { | ||
if (functions === undefined) { | ||
return undefined; | ||
} | ||
return functions.map((fun) => { | ||
const args = fun.parameters; | ||
let openAiProperties: { [key: string]: any } = {}; | ||
let required: string[] = []; | ||
|
||
if (args) { | ||
for (const [name, arg] of Object.entries(args)) { | ||
const description = arg.description; | ||
if (typeof arg.type === "string" || arg.type === undefined) { | ||
const type = arg.type || "string"; | ||
openAiProperties[name] = { | ||
type: arg.type, | ||
...(description ? { description } : {}), | ||
}; | ||
} else if (Array.isArray(arg.type)) { | ||
openAiProperties[name] = { | ||
type: "enum", | ||
enum: arg.type, | ||
...(description ? { description } : {}), | ||
}; | ||
} | ||
|
||
if (arg.optional !== true) { | ||
required.push(name); | ||
} | ||
} | ||
} | ||
|
||
return { | ||
name: fun.name, | ||
description: fun.description, | ||
parameters: { | ||
type: "object", | ||
properties: openAiProperties, | ||
...(required.length ? { required } : {}), | ||
}, | ||
}; | ||
}); | ||
} | ||
|
||
function functionCallToOpenAI(functionCall?: FunctionCall): any { | ||
if (functionCall === undefined) { | ||
return undefined; | ||
} | ||
return { | ||
name: functionCall.name, | ||
arguments: JSON.stringify(functionCall.arguments), | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.