Skip to content

Commit

Permalink
breaking: changed text/output/etc. functions to getters (#1035)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelgj authored Oct 16, 2024
1 parent 3d0b6a1 commit 6db94cd
Show file tree
Hide file tree
Showing 68 changed files with 217 additions and 222 deletions.
8 changes: 4 additions & 4 deletions docs/dotprompt.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const result = await greetingPrompt.generate({
},
});

console.log(result.text());
console.log(result.text);
```

Dotprompt's syntax is based on the [Handlebars](https://handlebarsjs.com/guide/)
Expand Down Expand Up @@ -183,7 +183,7 @@ const myPrompt = promptRef("myPrompt");
const result = await myPrompt.generate<typeof MySchema>({...});
// now strongly typed as MySchema
result.output();
result.output;
```

## Overriding Prompt Metadata
Expand Down Expand Up @@ -237,7 +237,7 @@ const menu = await createMenuPrompt.generate({
},
});
console.log(menu.output());
console.log(menu.output);
```

Output conformance is achieved by inserting additional instructions into the
Expand Down Expand Up @@ -340,7 +340,7 @@ const result = await describeImagePrompt.generate({
},
});
console.log(result.text());
console.log(result.text);
```

## Partials
Expand Down
2 changes: 1 addition & 1 deletion docs/evaluation.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export const synthesizeQuestions = defineFlow(
text: `Generate one question about the text below: ${chunks[i]}`,
},
});
questions.push(qResponse.text());
questions.push(qResponse.text);
}
return questions;
}
Expand Down
2 changes: 1 addition & 1 deletion docs/get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ so that it can be used outside of a Node project.
// Handle the response from the model API. In this sample, we just convert
// it to a string, but more complicated flows might coerce the response into
// structured output or chain the response into another LLM call, etc.
return llmResponse.text();
return llmResponse.text;
}
);
Expand Down
8 changes: 4 additions & 4 deletions docs/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ configureGenkit(/* ... */);
prompt: 'Invent a menu item for a pirate themed restaurant.',
});

console.log(await llmResponse.text());
console.log(await llmResponse.text);
})();
```

Expand Down Expand Up @@ -339,7 +339,7 @@ object's `output()` method:
```ts
type MenuItem = z.infer<typeof MenuItemSchema>;

const output: MenuItem | null = llmResponse.output();
const output: MenuItem | null = llmResponse.output;
```

#### Handling errors
Expand Down Expand Up @@ -425,7 +425,7 @@ Handle each of these chunks as they become available:
```ts
for await (const responseChunkData of llmResponseStream.stream()) {
const responseChunk = responseChunkData as GenerateResponseChunk;
console.log(responseChunk.text());
console.log(responseChunk.text);
}
```

Expand Down Expand Up @@ -454,7 +454,7 @@ const llmResponseStream = await generateStream({
for await (const responseChunkData of llmResponseStream.stream()) {
const responseChunk = responseChunkData as GenerateResponseChunk<Menu>;
// output() returns an object representing the entire output so far
const output: Menu | null = responseChunk.output();
const output: Menu | null = responseChunk.output;
console.log(output);
}
```
Expand Down
4 changes: 2 additions & 2 deletions docs/plugin-authoring-evaluator.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ export async function deliciousnessScore<
});

// Parse the output
const parsedResponse = response.output();
const parsedResponse = response.output;
if (!parsedResponse) {
throw new Error(`Unable to parse evaluator response: ${response.text()}`);
throw new Error(`Unable to parse evaluator response: ${response.text}`);
}

// Return a scored response
Expand Down
2 changes: 1 addition & 1 deletion docs/prompts.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const response = await (threeGreetingsPrompt.generate<typeof outputSchema>(
{ input: { name: 'Fred' } }
));

response.output()?.likeAPirate
response.output?.likeAPirate
// "Ahoy there, Fred! May the winds be ever in your favor!"
```

Expand Down
6 changes: 3 additions & 3 deletions docs/rag.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ export const menuQAFlow = defineFlow(
context: docs,
});

const output = llmResponse.text();
const output = llmResponse.text;
return output;
}
);
Expand Down Expand Up @@ -333,7 +333,7 @@ defineSimpleRetriever({
// and several keys to use as metadata
metadata: ['from', 'to', 'subject'],
} async (query, config) => {
const result = await searchEmails(query.text(), {limit: config.limit});
const result = await searchEmails(query.text, {limit: config.limit});
return result.data.emails;
});
```
Expand Down Expand Up @@ -433,7 +433,7 @@ export const rerankFlow = defineFlow(
});

return rerankedDocuments.map((doc) => ({
text: doc.text(),
text: doc.text,
score: doc.metadata.score,
}));
}
Expand Down
2 changes: 1 addition & 1 deletion genkit-tools/cli/config/firebase.index.ts.template
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ export const menuSuggestionFlow = onFlow(
// convert it to a string, but more complicated flows might coerce the
// response into structured output or chain the response into another
// LLM call, etc.
return llmResponse.text();
return llmResponse.text;
}
);
2 changes: 1 addition & 1 deletion genkit-tools/cli/config/nextjs.genkit.ts.template
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const menuSuggestionFlow = ai.defineFlow(
// convert it to a string, but more complicated flows might coerce the
// response into structured output or chain the response into another
// LLM call, etc.
return llmResponse.text();
return llmResponse.text;
}
);

Expand Down
2 changes: 1 addition & 1 deletion genkit-tools/cli/config/nodejs.index.ts.template
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ export const menuSuggestionFlow = ai.defineFlow(
// Handle the response from the model API. In this sample, we just convert
// it to a string, but more complicated flows might coerce the response into
// structured output or chain the response into another LLM call, etc.
return llmResponse.text();
return llmResponse.text;
}
);
2 changes: 1 addition & 1 deletion genkit-tools/common/src/utils/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ async function recordInternal(
if (!response.ok) {
logger.warn(`Analytics validation HTTP error: ${response.status}`);
}
const respBody = await response.text();
const respBody = await response.text;
logger.info(`Analytics validation result: ${respBody}`);
}
// response.ok / response.status intentionally ignored, see comment below.
Expand Down
4 changes: 2 additions & 2 deletions js/ai/src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class Document implements DocumentData {
* Concatenates all `text` parts present in the document with no delimiter.
* @returns A string of all concatenated text parts.
*/
text(): string {
get text(): string {
return this.content.map((part) => part.text || '').join('');
}

Expand All @@ -79,7 +79,7 @@ export class Document implements DocumentData {
* (for example) an image.
* @returns The first detected `media` part in the document.
*/
media(): { url: string; contentType?: string } | null {
get media(): { url: string; contentType?: string } | null {
return this.content.find((part) => part.media)?.media || null;
}

Expand Down
44 changes: 22 additions & 22 deletions js/ai/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ export class Message<T = unknown> implements MessageData {
*
* @returns The structured output contained in the message.
*/
output(): T {
return this.data() || extractJson<T>(this.text());
get output(): T {
return this.data || extractJson<T>(this.text);
}

toolResponseParts(): ToolResponsePart[] {
Expand All @@ -83,7 +83,7 @@ export class Message<T = unknown> implements MessageData {
* Concatenates all `text` parts present in the message with no delimiter.
* @returns A string of all concatenated text parts.
*/
text(): string {
get text(): string {
return this.content.map((part) => part.text || '').join('');
}

Expand All @@ -92,23 +92,23 @@ export class Message<T = unknown> implements MessageData {
* (for example) an image from a generation expected to create one.
* @returns The first detected `media` part in the message.
*/
media(): { url: string; contentType?: string } | null {
get media(): { url: string; contentType?: string } | null {
return this.content.find((part) => part.media)?.media || null;
}

/**
* Returns the first detected `data` part of a message.
* @returns The first `data` part detected in the message (if any).
*/
data(): T | null {
get data(): T | null {
return this.content.find((part) => part.data)?.data as T | null;
}

/**
* Returns all tool request found in this message.
* @returns Array of all tool request found in this message.
*/
toolRequests(): ToolRequestPart[] {
get toolRequests(): ToolRequestPart[] {
return this.content.filter(
(part) => !!part.toolRequest
) as ToolRequestPart[];
Expand Down Expand Up @@ -187,7 +187,7 @@ export class GenerateResponse<O = unknown> implements ModelResponseData {
}

if (request?.output?.schema || this.request?.output?.schema) {
const o = this.output();
const o = this.output;
parseSchema(o, {
jsonSchema: request?.output?.schema || this.request?.output?.schema,
});
Expand All @@ -211,17 +211,17 @@ export class GenerateResponse<O = unknown> implements ModelResponseData {
* @param index The candidate index from which to extract output. If not provided, finds first candidate that conforms to output schema.
* @returns The structured output contained in the selected candidate.
*/
output(): O | null {
return this.message?.output() || null;
get output(): O | null {
return this.message?.output || null;
}

/**
* Concatenates all `text` parts present in the candidate's message with no delimiter.
* @param index The candidate index from which to extract text, defaults to first candidate.
* @returns A string of all concatenated text parts.
*/
text(): string {
return this.message?.text() || '';
get text(): string {
return this.message?.text || '';
}

/**
Expand All @@ -230,26 +230,26 @@ export class GenerateResponse<O = unknown> implements ModelResponseData {
* @param index The candidate index from which to extract media, defaults to first candidate.
* @returns The first detected `media` part in the candidate.
*/
media(): { url: string; contentType?: string } | null {
return this.message?.media() || null;
get media(): { url: string; contentType?: string } | null {
return this.message?.media || null;
}

/**
* Returns the first detected `data` part of the selected candidate's message.
* @param index The candidate index from which to extract data, defaults to first candidate.
* @returns The first `data` part detected in the candidate (if any).
*/
data(): O | null {
return this.message?.data() || null;
get data(): O | null {
return this.message?.data || null;
}

/**
* Returns all tool request found in the candidate.
* @param index The candidate index from which to extract tool requests, defaults to first candidate.
* @returns Array of all tool request found in the candidate.
*/
toolRequests(): ToolRequestPart[] {
return this.message?.toolRequests() || [];
get toolRequests(): ToolRequestPart[] {
return this.message?.toolRequests || [];
}

/**
Expand Down Expand Up @@ -316,7 +316,7 @@ export class GenerateResponseChunk<T = unknown>
* Concatenates all `text` parts present in the chunk with no delimiter.
* @returns A string of all concatenated text parts.
*/
text(): string {
get text(): string {
return this.content.map((part) => part.text || '').join('');
}

Expand All @@ -325,23 +325,23 @@ export class GenerateResponseChunk<T = unknown>
* (for example) an image from a generation expected to create one.
* @returns The first detected `media` part in the chunk.
*/
media(): { url: string; contentType?: string } | null {
get media(): { url: string; contentType?: string } | null {
return this.content.find((part) => part.media)?.media || null;
}

/**
* Returns the first detected `data` part of a chunk.
* @returns The first `data` part detected in the chunk (if any).
*/
data(): T | null {
get data(): T | null {
return this.content.find((part) => part.data)?.data as T | null;
}

/**
* Returns all tool request found in this chunk.
* @returns Array of all tool request found in this chunk.
*/
toolRequests(): ToolRequestPart[] {
get toolRequests(): ToolRequestPart[] {
return this.content.filter(
(part) => !!part.toolRequest
) as ToolRequestPart[];
Expand All @@ -351,7 +351,7 @@ export class GenerateResponseChunk<T = unknown>
* Attempts to extract the longest valid JSON substring from the accumulated chunks.
* @returns The longest valid JSON substring found in the accumulated chunks.
*/
output(): T | null {
get output(): T | null {
if (!this.accumulatedChunks) return null;
const accumulatedText = this.accumulatedChunks
.map((chunk) => chunk.content.map((part) => part.text || '').join(''))
Expand Down
2 changes: 1 addition & 1 deletion js/ai/src/model/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ const CONTEXT_ITEM_TEMPLATE = (
} else if (options?.citationKey === undefined) {
out += `[${d.metadata?.['ref'] || d.metadata?.['id'] || index}]: `;
}
out += d.text() + '\n';
out += d.text + '\n';
return out;
};

Expand Down
Loading

0 comments on commit 6db94cd

Please sign in to comment.