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

Participant #526

Merged
merged 14 commits into from
Jun 10, 2024
11 changes: 11 additions & 0 deletions docs/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/core/src/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ export async function executeChatSession(
functions: ChatFunctionCallback[],
schemas: Record<string, JSONSchema>,
completer: ChatCompletionHandler,
chatParticipants: ChatParticipantHandler[],
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
genOptions: GenerationOptions
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
) {
const {
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/expander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ async function callExpander(
let functions: ChatFunctionCallback[] = []
let fileMerges: FileMergeHandler[] = []
let outputProcessors: PromptOutputProcessorHandler[] = []
let chatParticipants: ChatParticipantHandler[] = []
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The chatParticipants parameter should be placed after aici to match the order in the function signature.

generated by genaiscript pr-review-commit

pelikhan marked this conversation as resolved.
Show resolved Hide resolved
let aici: AICIRequest

const logCb = (msg: any) => {
Expand Down Expand Up @@ -139,6 +140,7 @@ async function callExpander(
functions: fns,
fileMerges: fms,
outputProcessors: ops,
chatParticipants: cps,
} = await renderPromptNode(model, node, { trace })
text = prompt
assistantText = assistantPrompt
Expand All @@ -147,6 +149,7 @@ async function callExpander(
functions = fns
fileMerges = fms
outputProcessors = ops
chatParticipants = cps
if (errors?.length) {
for (const error of errors) trace.error(``, error)
status = "error"
Expand Down Expand Up @@ -182,6 +185,7 @@ async function callExpander(
functions,
fileMerges,
outputProcessors,
chatParticipants,
aici,
}
}
Expand Down Expand Up @@ -313,6 +317,7 @@ export async function expandTemplate(
const functions = prompt.functions
const fileMerges = prompt.fileMerges
const outputProcessors = prompt.outputProcessors
const chatParticipants = prompt.chatParticipants

if (prompt.logs?.length) trace.details("📝 console.log", prompt.logs)
if (prompt.text) {
Expand Down Expand Up @@ -361,6 +366,7 @@ export async function expandTemplate(
if (sysr.functions) functions.push(...sysr.functions)
if (sysr.fileMerges) fileMerges.push(...sysr.fileMerges)
if (sysr.outputProcessors) outputProcessors.push(...outputProcessors)
if (sysr.chatParticipants) chatParticipants.push(...chatParticipants)
if (sysr.logs?.length) trace.details("📝 console.log", sysr.logs)
if (sysr.text) {
systemMessage.content += SYSTEM_FENCE + "\n" + sysr.text + "\n"
Expand Down Expand Up @@ -434,5 +440,6 @@ ${schemaTs}
responseSchema,
fileMerges,
outputProcessors,
chatParticipants,
}
}
11 changes: 11 additions & 0 deletions packages/core/src/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/core/src/promptcontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { readText } from "./fs"
import {
PromptNode,
appendChild,
createChatParticipant,
createFileMergeNode,
createImageNode,
createOutputProcessor,
Expand Down Expand Up @@ -169,6 +170,10 @@ export function createPromptContext(
if (fn) appendPromptChild(createOutputProcessor(fn))
}

const defChatParticipant = (participant: ChatParticipantHandler) => {
if (participant) appendPromptChild(createChatParticipant(participant))
}
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
pelikhan marked this conversation as resolved.
Show resolved Hide resolved

const promptHost: PromptHost = {
askUser: (question) =>
host.askUser({
Expand Down Expand Up @@ -204,6 +209,7 @@ export function createPromptContext(
retrieval,
host: promptHost,
defOutputProcessor,
defChatParticipant,
defFileMerge: (fn) => {
appendPromptChild(createFileMergeNode(fn))
},
Expand Down
25 changes: 24 additions & 1 deletion packages/core/src/promptdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface PromptNode extends ContextExpansionOptions {
| "stringTemplate"
| "assistant"
| "def"
| "chatParticipant"
| undefined
children?: PromptNode[]
error?: unknown
Expand Down Expand Up @@ -98,6 +99,11 @@ export interface PromptOutputProcessorNode extends PromptNode {
fn: PromptOutputProcessorHandler
}

export interface PromptChatParticipantNode extends PromptNode {
type: "chatParticipant"
participant: ChatParticipantHandler
}

export function createTextNode(
value: Awaitable<string>,
options?: ContextExpansionOptions
Expand Down Expand Up @@ -227,6 +233,12 @@ export function createOutputProcessor(
return { type: "outputProcessor", fn }
}

export function createChatParticipant(
participant: ChatParticipantHandler
): PromptChatParticipantNode {
return { type: "chatParticipant", participant }
}
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
pelikhan marked this conversation as resolved.
Show resolved Hide resolved

export function createDefDataNode(
name: string,
data: object | object[],
Expand Down Expand Up @@ -277,6 +289,7 @@ export interface PromptNodeVisitor {
stringTemplate?: (node: PromptStringTemplateNode) => Awaitable<void>
outputProcessor?: (node: PromptOutputProcessorNode) => Awaitable<void>
assistant?: (node: PromptAssistantNode) => Awaitable<void>
chatParticipant?: (node: PromptChatParticipantNode) => Awaitable<void>
}

export async function visitNode(node: PromptNode, visitor: PromptNodeVisitor) {
Expand Down Expand Up @@ -309,6 +322,9 @@ export async function visitNode(node: PromptNode, visitor: PromptNodeVisitor) {
case "assistant":
await visitor.assistant?.(node as PromptAssistantNode)
break
case "chatParticipant":
await visitor.chatParticipant?.(node as PromptChatParticipantNode)
break
}
if (node.error) visitor.error?.(node)
if (!node.error && node.children) {
Expand All @@ -328,6 +344,7 @@ export interface PromptNodeRender {
functions: ChatFunctionCallback[]
fileMerges: FileMergeHandler[]
outputProcessors: PromptOutputProcessorHandler[]
chatParticipants: ChatParticipantHandler[]
messages: ChatCompletionMessageParam[]
}

Expand Down Expand Up @@ -501,6 +518,7 @@ export async function renderPromptNode(
const functions: ChatFunctionCallback[] = []
const fileMerges: FileMergeHandler[] = []
const outputProcessors: PromptOutputProcessorHandler[] = []
const chatParticipants: ChatParticipantHandler[] = []

await visitNode(node, {
text: async (n) => {
Expand Down Expand Up @@ -589,7 +607,11 @@ ${trimNewlines(schemaText)}
},
outputProcessor: (n) => {
outputProcessors.push(n.fn)
trace.itemValue(`output processor`, n.fn)
trace.itemValue(`output processor`, n.fn.name)
},
chatParticipant: (n) => {
chatParticipants.push(n.participant)
trace.itemValue(`chat participant`, n.participant.name)
},
})

Expand All @@ -609,6 +631,7 @@ ${trimNewlines(schemaText)}
functions,
fileMerges,
outputProcessors,
chatParticipants,
errors,
messages,
}
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/promptrunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export async function runTemplate(
functions,
fileMerges,
outputProcessors,
chatParticipants,
status,
statusText,
temperature,
Expand Down Expand Up @@ -223,6 +224,7 @@ export async function runTemplate(
functions,
schemas,
completer,
chatParticipants,
genOptions
)
const {
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/runpromptcontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ export function createRunPromptContext(
let messages: ChatCompletionMessageParam[] = []
let functions: ChatFunctionCallback[] = undefined
let schemas: Record<string, JSONSchema> = undefined
let chatParticipants: ChatParticipantHandler[] = undefined
// expand template
const { provider } = parseModelIdentifier(genOptions.model)
if (provider === MODEL_PROVIDER_AICI) {
Expand All @@ -201,12 +202,14 @@ export function createRunPromptContext(
schemas: scs,
functions: fns,
messages: msgs,
chatParticipants: cps,
} = await renderPromptNode(genOptions.model, node, {
trace,
})

schemas = scs
functions = fns
chatParticipants = cps
messages.push(...msgs)
pelikhan marked this conversation as resolved.
Show resolved Hide resolved

if (errors?.length)
Expand Down Expand Up @@ -234,6 +237,7 @@ export function createRunPromptContext(
functions,
schemas,
completer,
chatParticipants,
genOptions
)
const { json, text } = resp
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/types/prompt_template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,10 @@ interface ChatFunctionCallback {
) => ChatFunctionCallOutput | Promise<ChatFunctionCallOutput>
}

type ChatParticipantHandler = (
messages: ChatCompletionMessageParam[]
) => Promise<string>

/**
* A set of text extracted from the context of the prompt execution
*/
Expand Down Expand Up @@ -1236,6 +1240,7 @@ interface RunPromptContext {
parameters: PromptParametersSchema | JSONSchema,
fn: ChatFunctionHandler
): void
defChatParticipant(participant: ChatParticipantHandler): void
console: PromptConsole
}

Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/types/prompt_type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ declare function runPrompt(
*/
declare function defOutputProcessor(fn: PromptOutputProcessorHandler): void

/**
* Registers a chat participant
* @param participant
*/
declare function defChatParticipant(participant: ChatParticipantHandler): void

/**
* @deprecated Use `defOutputProcessor` instead.
*/
Expand Down
11 changes: 11 additions & 0 deletions packages/sample/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading