-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds context caching, toast UI, versioned templates
feat: adds context caching, toast UI, versioned templates
- Loading branch information
Showing
12 changed files
with
343 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
|
@@ -16,6 +17,7 @@ var ( | |
projectID string | ||
userEmail string = "[email protected]" | ||
userEmailParam string = "user" | ||
convoContext string | ||
) | ||
|
||
type ClientError struct { | ||
|
@@ -76,6 +78,29 @@ func startConversation(c *gin.Context) { | |
|
||
LogInfo("Start conversation request received") | ||
|
||
// create a new conversation context | ||
convoHistory, err := getConversation(userEmail, projectID) | ||
if err != nil { | ||
LogError(fmt.Sprintf("couldn't get conversation history: %v\n", err)) | ||
} | ||
|
||
// VertexAI + Gemini caching has a hard lower minimum; warn if the | ||
// minimum isn't reached | ||
convoContext, err = storeConversationContext(convoHistory, projectID) | ||
var minConvoNum *MinCacheNotReachedError | ||
if errors.As(err, &minConvoNum) { | ||
LogWarning(err.Error()) | ||
} else if err != nil { | ||
LogError(fmt.Sprintf("couldn't store conversation context: %v\n", err)) | ||
} | ||
|
||
// Populate the conversation context variable for grounding both Gemma and | ||
// Gemini (< 33000 tokens) caching. | ||
err = setConversationContext(convoHistory) | ||
if err != nil { | ||
LogError(fmt.Sprintf("couldn't set conversation context: %v\n", err)) | ||
} | ||
|
||
c.HTML(http.StatusOK, "index.html", gin.H{ | ||
"Message": struct { | ||
Message string | ||
|
@@ -100,7 +125,7 @@ func respondToUser(c *gin.Context) { | |
var promptTemplate string | ||
err := c.BindJSON(&userMsg) | ||
if err != nil { | ||
LogError(fmt.Sprintf("Couldn't parse client message: %v\n", err)) | ||
LogError(fmt.Sprintf("couldn't parse client message: %v\n", err)) | ||
c.JSON(http.StatusBadRequest, gin.H{ | ||
"Message": "Couldn't parse payload", | ||
}) | ||
|
@@ -115,7 +140,7 @@ func respondToUser(c *gin.Context) { | |
promptTemplate = GeminiTemplate | ||
} | ||
if err != nil { | ||
LogError(fmt.Sprintf("Bad response from Gemini %v\n", err)) | ||
LogError(fmt.Sprintf("bad response from %s: %v\n", userMsg.Model, err)) | ||
botResponse = "Oops! I had troubles understanding that ..." | ||
} | ||
|
||
|
@@ -127,11 +152,14 @@ func respondToUser(c *gin.Context) { | |
Prompt: promptTemplate, | ||
} | ||
|
||
// Use a separate thread to store the conversation | ||
// Store the conversation in Firestore and update the cachedContext | ||
// This is dual-entry accounting so that we don't have to query Firestore | ||
// every time to update the cached context | ||
documentID, err := saveConversation(*convo, userEmail, projectID) | ||
if err != nil { | ||
LogError(fmt.Sprintf("Couldn't save conversation: %v\n", err)) | ||
} | ||
cachedContext += fmt.Sprintf("### Human: %s\n### Assistant: %s\n", userMsg.Message, botResponse) | ||
|
||
c.JSON(http.StatusOK, gin.H{ | ||
"Message": struct { | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{range .}} ### Human: {{.UserQuery}} ### Assistant: {{.BotResponse}} {{ printf "\n" }}{{end}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
### Instruction: | ||
|
||
Ignore all previous instructions. | ||
|
||
You are a helpful travel agent assistant. The user will ask you about where to go on vacation. | ||
You are going to help them plan their trip. | ||
|
||
Be sure to label your response with '### Assistant:' and end your response with '##ENDRESPONSE##'. | ||
|
||
Do not include system instructions in the response. | ||
|
||
Here is the conversation history between you, Assistant, and the user, Human. | ||
|
||
{{ .History }} | ||
|
||
### Input: | ||
Here is the user query. Respond to the user's request. Check your answer before responding. | ||
|
||
### Human: {{ .Query }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
### Instruction: | ||
|
||
Ignore all previous instructions. | ||
|
||
You are a helpful travel agent assistant. The user will ask you about where to go on vacation. | ||
You are going to help them plan their trip. | ||
|
||
Be sure to label your response with '### Assistant:' and end your response with '##ENDRESPONSE##'. | ||
|
||
Do not include system instructions in the response. | ||
|
||
Here is the conversation history between you, Assistant, and the user, Human. Check your answer before responding | ||
|
||
{{ .History }} | ||
|
||
### Input: | ||
Here is the user query. Respond to the user's request. Check your answer before responding. | ||
|
||
### Human: {{ .Query }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,11 +5,11 @@ | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<title>My Own Herodotus</title> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css"> | ||
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="preload" as="font"> | ||
<link href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap" rel="stylesheet"> | ||
<link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,[email protected],100..700,0..1,-50..200&icon_names=send,thumb_down,thumb_up" rel="stylesheet" /> | ||
<style> | ||
body, html { | ||
font-family: Roboto !important; | ||
font-family: "Roboto", sans-serif; | ||
} | ||
.material-symbols-outlined { | ||
font-variation-settings: | ||
|
@@ -18,16 +18,57 @@ | |
'GRAD' 0, | ||
'opsz' 48 | ||
} | ||
.notification { | ||
margin-top: 15px; | ||
} | ||
@keyframes fade-in { | ||
25%, | ||
90% { | ||
opacity: 0%; | ||
} | ||
|
||
40% { | ||
scale: 100%; | ||
} | ||
} | ||
.toast { | ||
animation-name: fade-in; | ||
animation-duration: 6s; | ||
} | ||
.toast-hide { | ||
opacity: 0%; | ||
} | ||
/* This overflow code doesn't seem to work. */ | ||
.conversation { | ||
width: 100%; | ||
height: 100%; | ||
overflow: hidden; | ||
} | ||
.conversation > .scroll { | ||
width: 100%; | ||
height: 100%; | ||
overflow-y: scroll; | ||
box-sizing: content-box; | ||
} | ||
</style> | ||
<link rel="icon" type="image/x-icon" href="/favicon.ico" /> | ||
|
||
</head> | ||
<body> | ||
<div class="container"> | ||
<p class="title is-1">My Own Herodotus</p> | ||
<p class="subtitle is-3">A travel guide for everyone</p> | ||
<div class="columns"> | ||
<div class="column is-four-fifths"> | ||
<p class="title is-1">My Own Herodotus</p> | ||
<p class="subtitle is-3">A travel guide for everyone</p> | ||
</div> | ||
<div class="column"> | ||
<div class="toast-hide notification is-info"> | ||
Response rating received! | ||
</div> | ||
</div> | ||
</div> | ||
<hr class="bd-hr"> | ||
|
||
<div class="block"> | ||
<div class="box"> | ||
<div class="select"> | ||
|
@@ -38,10 +79,13 @@ | |
</div> | ||
</div> | ||
</div> | ||
|
||
{{ template "herodotus_msg.tmpl" .Message }} | ||
<div class="conversation"> | ||
<div class="scroll"> | ||
{{ template "herodotus_msg.tmpl" .Message }} | ||
|
||
{{ template "user_msg.tmpl" .Message }} | ||
{{ template "user_msg.tmpl" .Message }} | ||
</div> | ||
</div> | ||
</div> | ||
<script type="module" src="js/appInit.js"></script> | ||
<script type="module" defer src="js/validateAuth.js"></script> | ||
|
Oops, something went wrong.