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

docs(ai-function-caller): add ts doc comments #26

Merged
merged 1 commit into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
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
15 changes: 0 additions & 15 deletions .github/workflows/assigned-pulls-todo.yml

This file was deleted.

18 changes: 0 additions & 18 deletions .github/workflows/fix-spelling.yml

This file was deleted.

25 changes: 0 additions & 25 deletions .github/workflows/mind-your-language.yml

This file was deleted.

15 changes: 0 additions & 15 deletions .github/workflows/open-issue-triage.yml

This file was deleted.

42 changes: 42 additions & 0 deletions packages/ai-function-caller/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,39 @@
} from "./types/Message.js";
import { AIFunction, AIFunctionCall } from "./types/AIFunction.js";

/**
* A class for calling AI functions and managing interactions.
*/
export class AIFunctionCaller {
public messages: Message[] = [];
public functions: AIFunction<any>[];

Check failure on line 15 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#L15

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

/**
* Constructs an AIFunctionCaller instance.
* @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 23 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#L23

[@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;
}

/**
* Logs a message to the message array.
* @param message - The message to log.
*/
private logMessage(message: Message): void {
this.messages.push(message);
}

/**
* Executes the AI model and returns the response.
* @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 42 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#L42

[@typescript-eslint/no-explicit-any] Unexpected any. Specify a different type.
model: "gpt-4-0613",
messages: this.messages,
functions: this.functions,
Expand All @@ -32,13 +48,18 @@
return response.choices[0].message;
}

/**
* Processes an AI function call and returns the response.
* @param aiFunctionCall - The AI function call to process.
* @returns A Promise resolving to MessageContent or AIFunctionCall.
*/

Check notice on line 55 in packages/ai-function-caller/src/index.ts

View workflow job for this annotation

GitHub Actions / Chat GPT Code Peer Review

Improve error handling

The functions `execute` and `processAIFunctionCall` should have proper error handling in case of any exceptions or rejections. It is recommended to wrap the code blocks inside a try-catch block and handle the errors appropriately.
private async processAiFunctionCall(
aiFunctionCall: AIFunctionCall,
): Promise<MessageContent | AIFunctionCall> {
const { name, arguments: args } = aiFunctionCall;
const { method } = this.functions.find(
(f) => f.name === name,
) as AIFunction<any>;

Check failure on line 62 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#L62

[@typescript-eslint/no-explicit-any] Unexpected any. Specify a different type.
const functionResponse = await method(args);
return this.processMessage(
"Function",
Expand All @@ -47,6 +68,13 @@
);
}

/**
* Processes a message, logs it, and returns the AI model's response.
* @param sender - The sender of the message.
* @param role - The role of the message sender.
* @param content - The content of the message.
* @returns A Promise resolving to MessageContent or AIFunctionCall.
*/
private async processMessage(
sender: MessageSender,
role: MessageRole,
Expand All @@ -62,7 +90,21 @@
return modelResponse;
}

/**
* Processes a user query and returns the response.
* @param userMessage - The user message to process.
* @returns A Promise resolving to the response.
*/

Check notice on line 97 in packages/ai-function-caller/src/index.ts

View workflow job for this annotation

GitHub Actions / Chat GPT Code Peer Review

Improve Naming Convention

Consider renaming `processUserQuery` to `processUserMessage` for consistency with other method names.
public async query(userMessage: string): Promise<any> {

Check failure on line 98 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#L98

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

export {
AIFunction,
AIFunctionCall,
Message,
MessageContent,
MessageRole,
MessageSender,
};
64 changes: 64 additions & 0 deletions packages/ai-function-caller/src/types/AIFunction.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,95 @@
/**
* Represents the name of an AI function.
*/
export type AIFunctionName = string;

/**
* Describes an AI function.
*/
export type AIFunctionDescription = string;

/**
* Represents the name of a parameter in an AI function.
*/
export type AIFunctionParameterName = string;

/**
* Enumerates the possible types for AI function properties.
*/
export type AIPropertyType = "string" | "integer" | "boolean" | "object";

/**
* Represents the name of a property in an AI function.
*/
export type AIFunctionPropertyName = string;

/**
* Describes a property in an AI function.
*/
export type AIPropertyDescription = string;

/**
* Represents a property of an AI function, including its type and description.
*/
export type AIFunctionProperty = {
/** The type of the property. */
type: AIPropertyType;
/** The description of the property. */
description: AIPropertyDescription;
};

/**
* Represents the parameters of an AI function, including their types and required status.
*/
export type AIFunctionParameters = {
/** The type of the parameters, typically an object. */
type: "object";
/** The properties of the parameters, keyed by the parameter name. */
properties: {
[key: AIFunctionParameterName]: AIFunctionProperty;
};
/** An array of parameter names that are required. */
required: AIFunctionParameterName[];
};

/**
* Represents a parameter value in an AI function call, which can be a string, number, or boolean.
*/
export type AIFunctionParameter = string | number | boolean;

/**
* Represents a call to an AI function, including the function name and arguments.
*/
export type AIFunctionCall = {
/** The name of the AI function being called. */

Check notice on line 64 in packages/ai-function-caller/src/types/AIFunction.ts

View workflow job for this annotation

GitHub Actions / Chat GPT Code Peer Review

Incomplete documentation

The documentation for the 'name' property of the AIFunctionCall type is incomplete. It doesn't indicate the data type of the property or any additional details.
name: AIFunctionName;
/** The arguments passed to the AI function. */

Check notice on line 66 in packages/ai-function-caller/src/types/AIFunction.ts

View workflow job for this annotation

GitHub Actions / Chat GPT Code Peer Review

Improvement suggestion

The description for the 'arguments' property could be improved to specify the type of the arguments.
arguments: AIFunctionCallArguments;
};

/**
* Represents the arguments passed in an AI function call.
*/
export type AIFunctionCallArguments = string;

/**
* Represents an example usage of an AI function.
*/
export type AIFunctionExample = string;

/**
* Represents an AI function, including its name, description, parameters, and the method to execute it.
* @template AiFunctionCallResponse The expected response type from the AI function call.
*/
export type AIFunction<AiFunctionCallResponse> = {
/** The name of the AI function. */
name: AIFunctionName;
/** A description of the AI function. */
description: AIFunctionDescription;
/** The parameters required by the AI function. */
parameters: AIFunctionParameters;
/** The method to execute the AI function. */
method: (args: AIFunctionCallArguments) => Promise<AiFunctionCallResponse>;
/** An optional example of how the AI function can be used. */
example?: AIFunctionExample;
};
23 changes: 23 additions & 0 deletions packages/ai-function-caller/src/types/Message.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
import { AIFunctionCall } from "./AIFunction.js";

/**
* Represents a message in the system.
*/
export type Message = {
/** The role of the message sender. */
role: MessageRole;
/** The name of the message sender. */
name: MessageSender;
/** The content of the message, which can be null. */
content: MessageContent | null;
/** Optional AI function call associated with the message. */

Check notice on line 13 in packages/ai-function-caller/src/types/Message.ts

View workflow job for this annotation

GitHub Actions / Chat GPT Code Peer Review

Missing type declaration for AI function call

Please add the type declaration for the AI function call in the 'Message' type.
function_call?: AIFunctionCall;
};

/**
* Defines the content of a message, represented as a string.
*/
export type MessageContent = string;

/**
* Enumerates the possible roles of a message sender.
*/
export enum MessageRole {
/** Represents a function's message. */
Function = "function",
/** Represents a system's message. */
System = "system",
/** Represents a user's message. */
User = "user",
/** Represents an assistant's message. */
Assistant = "assistant",
}

/**
* Represents the sender of a message, identified by a string.
*/
export type MessageSender = string;
10 changes: 8 additions & 2 deletions packages/state-store/src/types/Interceptor.type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
/**
* Interceptor function for modifying state values before they are set.
* @template ValueType The type of value the interceptor accepts.
* This type defines an interceptor function for modifying state values before they are stored.
* An interceptor function accepts an initial state value and returns a potentially modified state value.
* The function has a single parameter: `value` of generic type `ValueType`.
* It is designed to modify `ValueType` values before they are set to the state.
*
* @template ValueType The type of value accepted by the interceptor. It serves as an input type for the interception function and also as its return type.
* @param {ValueType} value The initial state value which can be modified by the interceptor function.
* @returns {ValueType} Returns the potentially modified state value.
*/
export type Interceptor<ValueType> = (value: ValueType) => ValueType;
8 changes: 7 additions & 1 deletion packages/state-store/src/types/Interceptors.type.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { Interceptor } from "./Interceptor.type.js";

/**
* Type definition for interceptors. Keyed by state property names.
* This type defines a structure for storing multiple interceptors based on state property names.
* The `Interceptors` type is an object where each key is a property name from the `StateType` and the value is an optional array of `Interceptor` functions.
* It is particularly useful in scenarios where you want to apply one or more interceptors to different properties of your state.
*
* @template StateType This is the state type whose keys are used to associate interceptors.
* @property {Interceptor<StateType[Property]>[]} This optional property is an array of `Interceptor` functions associated with each `StateType` property.

Check failure on line 9 in packages/state-store/src/types/Interceptors.type.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/state-store/src/types/Interceptors.type.ts#L9

[prettier/prettier] Delete `··`
* Its key is derived using `keyof StateType` which provides the set of keys of an object.
*/
export type Interceptors<StateType> = {
[Property in keyof StateType]?: Interceptor<StateType[Property]>[];
Expand Down
Loading