Skip to content

Commit

Permalink
fix: Add Context Cache support for ChatSessionPreview class (#433)
Browse files Browse the repository at this point in the history
* add content cache to chat session

* remove logs, add gitignore

---------

Co-authored-by: Oleksii Oleniuk <[email protected]>
Co-authored-by: Yvonne Yu <[email protected]>
  • Loading branch information
3 people authored Oct 8, 2024
1 parent 0e9a08d commit f8a3bdf
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
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

0 comments on commit f8a3bdf

Please sign in to comment.