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

fix: Add Context Cache support for ChatSessionPreview class #433

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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ docs/

# Ignore files generated automatically.
test/spec/sponge_log.xml

# Ignore JetBrains IDE files
.idea/
7 changes: 5 additions & 2 deletions src/models/chat_session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ export class ChatSessionPreview {
private readonly toolConfig?: ToolConfig;
private readonly apiEndpoint?: string;
private readonly systemInstruction?: Content;
private readonly cachedContent?: string;

async getHistory(): Promise<Content[]> {
return Promise.resolve(this.historyInternal);
Expand All @@ -295,6 +296,7 @@ export class ChatSessionPreview {
this.toolConfig = request.toolConfig;
this.apiEndpoint = request.apiEndpoint;
this.requestOptions = requestOptions ?? {};
this.cachedContent = request.cachedContent;
if (request.systemInstruction) {
this.systemInstruction = formulateSystemInstructionIntoContent(
request.systemInstruction
Expand Down Expand Up @@ -423,20 +425,21 @@ export class ChatSessionPreview {
): Promise<StreamGenerateContentResult> {
const newContent: Content[] =
formulateNewContentFromSendMessageRequest(request);
const generateContentrequest: GenerateContentRequest = {
const generateContentRequest: GenerateContentRequest = {
contents: this.historyInternal.concat(newContent),
safetySettings: this.safetySettings,
generationConfig: this.generationConfig,
tools: this.tools,
toolConfig: this.toolConfig,
systemInstruction: this.systemInstruction,
cachedContent: this.cachedContent,
};

const streamGenerateContentResultPromise = generateContentStream(
this.location,
this.resourcePath,
this.fetchToken(),
generateContentrequest,
generateContentRequest,
this.apiEndpoint,
this.generationConfig,
this.safetySettings,
Expand Down
Loading