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

hacks i did to make demo work #1219

Draft
wants to merge 1 commit into
base: next
Choose a base branch
from
Draft
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
7 changes: 5 additions & 2 deletions js/ai/src/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,16 @@ export async function lookupToolByName(
registry: Registry,
name: string
): Promise<ToolAction> {
let tool =
(await registry.lookupAction(name)) ||
await registry.initializeAllPlugins();
const tool =
(await registry.lookupAction(`/tool/${name}`)) ||
(await registry.lookupAction(name)) ||
(await registry.lookupAction(`/prompt/${name}`));

if (!tool) {
throw new Error(`Tool ${name} not found`);
}
console.log(tool.__action.name, ':', tool.__action.description);
return tool as ToolAction;
}

Expand Down
13 changes: 11 additions & 2 deletions js/genkit/src/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export class Chat {
messages?: MessageData[];
}
) {
console.log('Chat CONSTRUCTOR');
this.sessionId = options.id;
this.threadName = options.thread;
this.requestBase = requestBase?.then((rb) => {
Expand All @@ -105,9 +106,10 @@ export class Chat {
}
requestBase.messages = [...(requestBase.messages ?? []), promptMessage];
}

requestBase.messages = [
...(options.messages ?? []),
...(requestBase.messages ?? []),
...(options.messages?.filter((m) => !m.metadata?.preamble) ?? []),
];
this._messages = requestBase.messages;
return requestBase;
Expand All @@ -121,6 +123,7 @@ export class Chat {
>(
options: string | Part[] | ChatGenerateOptions<O, CustomOptions>
): Promise<GenerateResponse<z.infer<O>>> {
console.log('TOP OF SEND');
return runWithSession(this.session, () =>
runInNewSpan({ metadata: { name: 'send' } }, async () => {
let resolvedOptions;
Expand All @@ -140,11 +143,13 @@ export class Chat {
resolvedOptions = options as ChatGenerateOptions<O, CustomOptions>;
streamingCallback = resolvedOptions.streamingCallback;
}
console.log('ABOUT TO AWAIT REQUESTBASE');
let request: GenerateOptions = {
...(await this.requestBase),
messages: this.messages,
...resolvedOptions,
};
console.log('REQUEST:', request);
let response = await this.genkit.generate({
...request,
streamingCallback,
Expand Down Expand Up @@ -185,11 +190,15 @@ export class Chat {
resolvedOptions = options as GenerateStreamOptions<O, CustomOptions>;
}

console.log('ABOUT TO CALL GENERATESTREAM');
const resolvedRequest = await this.requestBase;
console.log('MESSAGES:', this.messages);
const { response, stream } = await this.genkit.generateStream({
...(await this.requestBase),
...resolvedRequest,
messages: this.messages,
...resolvedOptions,
});
console.log('IT GOT CALLED');

return {
response: response.finally(async () => {
Expand Down
11 changes: 9 additions & 2 deletions js/genkit/src/genkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ export class Genkit {
* Create a session for this environment.
*/
createSession<S = any>(options?: SessionOptions<S>): Session<S> {
const sessionId = uuidv4();
const sessionId = options?.sessionId || uuidv4();
const sessionData: SessionData = {
id: sessionId,
state: options?.initialState,
Expand All @@ -973,12 +973,19 @@ export class Genkit {
*/
async loadSession(
sessionId: string,
options: SessionOptions
options: SessionOptions & { createIfMissing?: boolean }
): Promise<Session> {
if (!options.store) {
throw new Error('options.store is required');
}
const sessionData = await options.store.get(sessionId);
if (!sessionData && options.createIfMissing) {
return this.createSession({
sessionId,
initialState: options.initialState,
store: options.store,
});
}

return new Session(this, {
id: sessionId,
Expand Down
4 changes: 4 additions & 0 deletions js/plugins/dotprompt/src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export interface PromptMetadata<
*/
export const PromptFrontmatterSchema = z.object({
name: z.string().optional(),
description: z.string().optional(),
variant: z.string().optional(),
model: z.string().optional(),
tools: z.array(z.string()).optional(),
Expand Down Expand Up @@ -135,6 +136,7 @@ export function toMetadata(
const fm = parseSchema<z.infer<typeof PromptFrontmatterSchema>>(attributes, {
schema: PromptFrontmatterSchema,
});
console.log('PARSED FM:', fm);

let input: PromptMetadata['input'] | undefined;
if (fm.input) {
Expand All @@ -154,6 +156,7 @@ export function toMetadata(

return stripUndefinedOrNull({
name: fm.name,
description: fm.description,
variant: fm.variant,
model: fm.model,
config: fm.config,
Expand All @@ -167,6 +170,7 @@ export function toMetadata(
export function toFrontmatter(md: PromptMetadata): PromptFrontmatter {
return stripUndefinedOrNull({
name: md.name,
description: md.description,
variant: md.variant,
model: typeof md.model === 'string' ? md.model : md.model?.name,
config: md.config,
Expand Down
23 changes: 21 additions & 2 deletions js/plugins/dotprompt/src/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,26 @@ export class Dotprompt<I = unknown> implements PromptMetadata<z.ZodTypeAny> {
return this._render(
{ ...this.input?.default, ...input },
options,
sessionStateData
sessionStateData || {
state: {
parentId: 4112,
parentName: 'Francis Example',
students: [
{
id: 3734,
name: 'Evelyn Example',
grade: 9,
activities: ['Choir', 'Drama Club'],
},
{
id: 9433,
name: 'Evan Example',
grade: 11,
activities: ['Chess Club'],
},
],
},
}
);
}

Expand All @@ -206,7 +225,7 @@ export class Dotprompt<I = unknown> implements PromptMetadata<z.ZodTypeAny> {
this.registry,
{
name: registryDefinitionKey(this.name, this.variant, options?.ns),
description: options?.description ?? 'Defined by Dotprompt',
description: options?.description,
inputSchema: this.input?.schema,
inputJsonSchema: this.input?.jsonSchema,
metadata: {
Expand Down
4 changes: 3 additions & 1 deletion js/plugins/dotprompt/src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,12 @@ export function loadPrompt(
variant = parts[1];
}
const source = readFileSync(join(path, filename), 'utf8');

console.log('LOAD PROMPT:', name);
const prompt = Dotprompt.parse(registry, name, source);
if (variant) {
prompt.variant = variant;
}
prompt.define({ ns: `dotprompt` });
prompt.define();
return prompt;
}
Loading