Skip to main content

Module: core/completion

This module provides the core completion components for AI.JSX.

Interfaces

Type Aliases

FunctionParameters

Ƭ FunctionParameters: Record<string, PlainFunctionParameter> | z.ZodObject<any>

Represents parameters to a FunctionDefinition.

This type allows two ways for specifying parameters:

  • For simple use cases, a record of parameter names to PlainFunctionParameter objects.
  • For more complex use cases, a z.ZodObject schema object (zod is a standard runtime type definition & checking library).

Note

If using a Zod schema, the top-level schema must be an object as per OpenAI specifications: https://platform.openai.com/docs/api-reference/chat/create#chat/create-parameters

For example, to describe a list of strings, the following is not accepted: const schema: z.Schema = z.array(z.string())

Instead, you can wrap it in an object like so: const schema: z.ZodObject = z.object({ arr: z.array(z.string()) })

Defined in

packages/ai-jsx/src/core/completion.tsx:98


ModelComponent

Ƭ ModelComponent<T>: Component<T>

A Component that invokes a Large Language Model.

Type parameters

NameType
Textends ModelPropsWithChildren

Defined in

packages/ai-jsx/src/core/completion.tsx:38


ModelPropsWithChildren

Ƭ ModelPropsWithChildren: ModelProps & { children: Node }

Represents a ModelProps with child @{link Node}s.

Defined in

packages/ai-jsx/src/core/completion.tsx:31

Functions

AssistantMessage

AssistantMessage(«destructured»): Node

Provide an Assistant Message to the LLM, for use within a ChatCompletion.

The assistant message tells the model what it has previously said. See https://platform.openai.com/docs/guides/gpt/chat-completions-api for more detail.

Example

   <ChatCompletion>
<UserMessage>I'd like to cancel my account.</UserMessage>
<AssistantMessage>Sorry to hear that. Can you tell me why?</AssistantMessage>
<UserMessage>It's too expensive.</UserMessage>
</ChatCompletion>

==> "Ok, thanks for that feedback. I'll cancel your account."

Parameters

NameType
«destructured»Object
› childrenNode

Returns

Node

Defined in

packages/ai-jsx/src/core/completion.tsx:258


ChatCompletion

ChatCompletion(«destructured», «destructured»): Element

Perform a Large Language Model call to do chat completion.

Every child of ChatCompletion must something that renders to a SystemMessage, UserMessage, or AssistantMessage.

Example

   function MyUserMessage() {
return <UserMessage>Hi, I'm a user message.</UserMessage>;
}

<ChatCompletion>
<SystemMessage>You are a nice person.</SystemMessage>
<MyUserMessage />
</ChatCompletion>

Parameters

NameType
«destructured»ModelProps & { children: Node } & Record<string, unknown>
«destructured»RenderContext

Returns

Element

Defined in

packages/ai-jsx/src/core/completion.tsx:367


ChatProvider

ChatProvider<T>(«destructured», «destructured»): Element

A ChatProvider is used by ChatCompletion to access an underlying Large Language Model.

Type parameters

NameType
Textends ModelPropsWithChildren

Parameters

NameType
«destructured»{ component?: ModelComponent<T> } & T
«destructured»RenderContext

Returns

Element

Defined in

packages/ai-jsx/src/core/completion.tsx:191


Completion

Completion(«destructured», «destructured»): Element

Perform a Large Language Mokdel call to do a completion.

In general, you should prefer to use ChatCompletion instead of Completion, because ChatCompletion uses better models.

Example

   <Completion>
Here's a list of three dog names:
</Completion>

==> 'Dottie, Murphy, Lucy'

Parameters

NameType
«destructured»ModelProps & { children: Node } & Record<string, unknown>
«destructured»RenderContext

Returns

Element

Defined in

packages/ai-jsx/src/core/completion.tsx:338


CompletionProvider

CompletionProvider<T>(«destructured», «destructured»): Element

A CompletionProvider is used by ChatCompletion to access an underlying Large Language Model.

Type parameters

NameType
Textends ModelPropsWithChildren

Parameters

NameType
«destructured»{ component?: ModelComponent<T> } & T
«destructured»RenderContext

Returns

Element

Defined in

packages/ai-jsx/src/core/completion.tsx:168


ConversationHistory

ConversationHistory(«destructured»): Element[]

Parameters

NameType
«destructured»Object
› messagesChatCompletionResponseMessage[]

Returns

Element[]

Defined in

packages/ai-jsx/src/core/completion.tsx:262


FunctionCall

FunctionCall(«destructured»): string

Provide a function call to the LLM, for use within a ChatCompletion.

The function call tells the model that a function was previously invoked by the model. See https://platform.openai.com/docs/guides/gpt/chat-completions-api for more detail. When the model returns a function call, @{link ChatCompletion} returns a @{link FunctionCall} component.

Example

   <ChatCompletion>
<UserMessage>What is 258 * 322?</UserMessage>
<FunctionCall name="evaluate_math" args={expression: "258 * 322"} />
<FunctionResponse name="evaluate_math">83076</FunctionResponse>
</ChatCompletion>

==> "That would be 83,076."

Parameters

NameType
«destructured»Object
› argsRecord<string, null | string | number | boolean>
› namestring

Returns

string

Defined in

packages/ai-jsx/src/core/completion.tsx:296


FunctionResponse

FunctionResponse(«destructured», «destructured»): Promise<string>

Renders to the output of a previous FunctionCall component, for use within a ChatCompletion.

See https://platform.openai.com/docs/guides/gpt/chat-completions-api for more detail.

Example

   <ChatCompletion>
<UserMessage>What is 258 * 322?</UserMessage>
<FunctionCall name="evaluate_math" args={expression: "258 * 322"} />
<FunctionResponse name="evaluate_math">83076</FunctionResponse>
</ChatCompletion>

==> "That would be 83,076."

Parameters

NameType
«destructured»Object
› childrenNode
› namestring
«destructured»ComponentContext

Returns

Promise<string>

Defined in

packages/ai-jsx/src/core/completion.tsx:316


SystemMessage

SystemMessage(«destructured»): Node

Provide a System Message to the LLM, for use within a ChatCompletion.

The system message can be used to put the model in character. See https://platform.openai.com/docs/guides/gpt/chat-completions-api for more detail.

Example

   <ChatCompletion>
<SystemMessage>You are a helpful customer service agent.</SystemMessage>
</ChatCompletion>

Parameters

NameType
«destructured»Object
› childrenNode

Returns

Node

Defined in

packages/ai-jsx/src/core/completion.tsx:220


UserMessage

UserMessage(«destructured»): Node

Provide a User Message to the LLM, for use within a ChatCompletion.

The user message tells the model what the user has said. See https://platform.openai.com/docs/guides/gpt/chat-completions-api for more detail.

Example

   <ChatCompletion>
<UserMessage>I'd like to cancel my account.</UserMessage>
</ChatCompletion>

==> 'Sorry to hear that. Can you tell me why?

Parameters

NameType
«destructured»Object
› childrenNode
› name?string

Returns

Node

Defined in

packages/ai-jsx/src/core/completion.tsx:238


getParametersSchema

getParametersSchema(parameters): JsonSchema7Meta & {} | { properties: {} ; required: string[] ; type: string = 'object' }

This function creates a JSON Schema object to describe parameters for a FunctionDefinition.

See FunctionParameters for more information on what parameters are supported.

Parameters

NameType
parametersFunctionParameters

Returns

JsonSchema7Meta & {} | { properties: {} ; required: string[] ; type: string = 'object' }

Defined in

packages/ai-jsx/src/core/completion.tsx:54