Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Devel #28

Merged
merged 3 commits into from
Nov 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/ai-function-caller/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,27 @@

/**
* A class for calling AI functions and managing interactions.
* @example
* ```ts
* import { AIFunctionCaller } from "@withorbit/ai-function-caller";
* import { AIFunction } from "@withorbit/ai-function-caller";
*
* const functions: AIFunction<any>[] = [
* {
* name: "add",
* method: async (args: any) => {
* return args.a + args.b;
* }
* ];
*
* const aiFunctionCaller = new AIFunctionCaller(functions);
* const response = await aiFunctionCaller.query("What is 2 + 2?");
* console.log(response); // 4
* ```
*/
export class AIFunctionCaller {
public messages: Message[] = [];
public functions: AIFunction<any>[];

Check failure on line 32 in packages/ai-function-caller/src/index.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/ai-function-caller/src/index.ts#L32

[@typescript-eslint/no-explicit-any] Unexpected any. Specify a different type.
private openai: OpenAI;

/**
Expand All @@ -20,7 +37,7 @@
* @param functions - Array of AI functions.
* @param initialMessages - Initial set of messages, defaults to an empty array.
*/
constructor(functions: AIFunction<any>[], initialMessages: Message[] = []) {

Check failure on line 40 in packages/ai-function-caller/src/index.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/ai-function-caller/src/index.ts#L40

[@typescript-eslint/no-explicit-any] Unexpected any. Specify a different type.
this.functions = functions;
this.openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
this.messages = initialMessages;
Expand All @@ -29,6 +46,7 @@
/**
* Logs a message to the message array.
* @param message - The message to log.
* @example
*/
private logMessage(message: Message): void {
this.messages.push(message);
Expand All @@ -39,7 +57,7 @@
* @returns A Promise resolving to MessageContent or AIFunctionCall.
*/
private async executeAIModel(): Promise<MessageContent | AIFunctionCall> {
const response: any = await this.openai.chat.completions.create({

Check failure on line 60 in packages/ai-function-caller/src/index.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/ai-function-caller/src/index.ts#L60

[@typescript-eslint/no-explicit-any] Unexpected any. Specify a different type.
model: "gpt-4-0613",
messages: this.messages,
functions: this.functions,
Expand All @@ -59,7 +77,7 @@
const { name, arguments: args } = aiFunctionCall;
const { method } = this.functions.find(
(f) => f.name === name,
) as AIFunction<any>;

Check failure on line 80 in packages/ai-function-caller/src/index.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/ai-function-caller/src/index.ts#L80

[@typescript-eslint/no-explicit-any] Unexpected any. Specify a different type.
const functionResponse = await method(args);
return this.processMessage(
"Function",
Expand Down Expand Up @@ -94,8 +112,13 @@
* Processes a user query and returns the response.
* @param userMessage - The user message to process.
* @returns A Promise resolving to the response.
* @example
* ```ts
* const response = await aiFunctionCaller.query("What is 2 + 2?");
* console.log(response); // 4
* ```
*/
public async query(userMessage: string): Promise<any> {

Check failure on line 121 in packages/ai-function-caller/src/index.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/ai-function-caller/src/index.ts#L121

[@typescript-eslint/no-explicit-any] Unexpected any. Specify a different type.
return await this.processMessage("User", MessageRole.User, userMessage);
}
}
Expand Down
Loading