-
Notifications
You must be signed in to change notification settings - Fork 2
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
Improved sliding window - trim old context #12
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
879424e
tweak to try and adjust when the function is called
conceptd3v 7b30aad
only send embedding for the most recent query
conceptd3v b8a9b1a
bugfix: groundings for first message exceed token limit
conceptd3v 3c28409
remove unnecessary object creation
conceptd3v 6461622
simplify kotlin syntax
conceptdev e8e8cbb
Update logic to allow for possibly null text field
conceptdev ba23d5c
don't repeat null check
conceptdev 61369bc
implement feedback
conceptd3v c63b17f
improve error handling for exceeding token limit
conceptd3v File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -16,6 +16,7 @@ import com.aallam.openai.api.image.ImageCreation | |||||||
import com.aallam.openai.api.image.ImageURL | ||||||||
import com.aallam.openai.api.model.ModelId | ||||||||
import com.aallam.openai.client.OpenAI | ||||||||
import com.example.compose.jetchat.data.CustomChatMessage | ||||||||
import com.example.compose.jetchat.data.DroidconContract | ||||||||
import com.example.compose.jetchat.data.DroidconDbHelper | ||||||||
import com.example.compose.jetchat.data.DroidconSessionData | ||||||||
|
@@ -25,6 +26,7 @@ import com.example.compose.jetchat.functions.AskDatabaseFunction | |||||||
import com.example.compose.jetchat.functions.ListFavoritesFunction | ||||||||
import com.example.compose.jetchat.functions.RemoveFavoriteFunction | ||||||||
import com.example.compose.jetchat.functions.SessionsByTimeFunction | ||||||||
import kotlinx.serialization.json.JsonNull.content | ||||||||
import kotlinx.serialization.json.jsonPrimitive | ||||||||
import java.time.LocalDateTime | ||||||||
import java.time.format.DateTimeFormatter | ||||||||
|
@@ -47,7 +49,7 @@ infix fun DoubleArray.dot(other: DoubleArray): Double { | |||||||
@OptIn(BetaOpenAI::class) | ||||||||
class DroidconEmbeddingsWrapper(val context: Context?) { | ||||||||
private val openAIToken: String = Constants.OPENAI_TOKEN | ||||||||
private var conversation: MutableList<ChatMessage> = mutableListOf() | ||||||||
private var conversation: MutableList<CustomChatMessage> = mutableListOf() | ||||||||
private var openAI: OpenAI = OpenAI(openAIToken) | ||||||||
/** Sqlite access for favorites, embeddings, and SQL queries */ | ||||||||
private val dbHelper = DroidconDbHelper(context) | ||||||||
|
@@ -56,11 +58,11 @@ class DroidconEmbeddingsWrapper(val context: Context?) { | |||||||
* then loaded into memory on first use */ | ||||||||
private var vectorCache: MutableMap<String, DoubleArray> = mutableMapOf() | ||||||||
|
||||||||
private var systemMessage: ChatMessage | ||||||||
private var systemMessage: CustomChatMessage | ||||||||
init { | ||||||||
systemMessage = ChatMessage( | ||||||||
systemMessage = CustomChatMessage ( | ||||||||
role = ChatRole.System, | ||||||||
content = """You are a personal assistant called JetchatAI. | ||||||||
grounding = """You are a personal assistant called JetchatAI. | ||||||||
You will answer questions about the speakers and sessions at the droidcon SF conference. | ||||||||
The conference is on June 8th and 9th, 2023 on the UCSF campus in Mission Bay. | ||||||||
It starts at 8am and finishes by 6pm. | ||||||||
|
@@ -81,9 +83,10 @@ class DroidconEmbeddingsWrapper(val context: Context?) { | |||||||
|
||||||||
// add the user's message to the chat history | ||||||||
conversation.add( | ||||||||
ChatMessage( | ||||||||
CustomChatMessage( | ||||||||
role = ChatRole.User, | ||||||||
content = messagePreamble + message | ||||||||
grounding = messagePreamble, | ||||||||
userContent = message | ||||||||
) | ||||||||
) | ||||||||
|
||||||||
|
@@ -134,9 +137,9 @@ class DroidconEmbeddingsWrapper(val context: Context?) { | |||||||
// no function, add the response to the conversation history | ||||||||
Log.i("LLM", "No function call was made, showing LLM response") | ||||||||
conversation.add( | ||||||||
ChatMessage( | ||||||||
CustomChatMessage( | ||||||||
role = ChatRole.Assistant, | ||||||||
content = chatResponse | ||||||||
userContent = chatResponse | ||||||||
) | ||||||||
) | ||||||||
} else { // handle function call | ||||||||
|
@@ -196,19 +199,19 @@ class DroidconEmbeddingsWrapper(val context: Context?) { | |||||||
handled = false | ||||||||
chatResponse += " " + function.name + " " + function.arguments | ||||||||
conversation.add( | ||||||||
ChatMessage( | ||||||||
CustomChatMessage( | ||||||||
role = ChatRole.Assistant, | ||||||||
content = chatResponse | ||||||||
userContent = chatResponse | ||||||||
) | ||||||||
) | ||||||||
} | ||||||||
} | ||||||||
if (handled) { | ||||||||
// add the 'call a function' response to the history | ||||||||
conversation.add( | ||||||||
ChatMessage( | ||||||||
CustomChatMessage( | ||||||||
role = completionMessage.role, | ||||||||
content = completionMessage.content | ||||||||
userContent = completionMessage.content | ||||||||
?: "", // required to not be empty in this case | ||||||||
functionCall = completionMessage.functionCall | ||||||||
) | ||||||||
|
@@ -217,10 +220,10 @@ class DroidconEmbeddingsWrapper(val context: Context?) { | |||||||
// add the response to the 'function' call to the history | ||||||||
// so that the LLM can form the final user-response | ||||||||
conversation.add( | ||||||||
ChatMessage( | ||||||||
CustomChatMessage( | ||||||||
role = ChatRole.Function, | ||||||||
name = function.name, | ||||||||
content = functionResponse | ||||||||
userContent = functionResponse | ||||||||
) | ||||||||
) | ||||||||
|
||||||||
|
@@ -239,9 +242,9 @@ class DroidconEmbeddingsWrapper(val context: Context?) { | |||||||
chatResponse = functionCompletion.choices.first().message?.content!! | ||||||||
// ignore trimmedConversation, will be recreated | ||||||||
conversation.add( | ||||||||
ChatMessage( | ||||||||
CustomChatMessage( | ||||||||
role = ChatRole.Assistant, | ||||||||
content = chatResponse | ||||||||
userContent = chatResponse | ||||||||
) | ||||||||
) | ||||||||
} | ||||||||
|
@@ -296,17 +299,20 @@ class DroidconEmbeddingsWrapper(val context: Context?) { | |||||||
Log.i("LLM", "Top match was ${sortedVectors.lastKey()} which was below 0.8 and failed to meet criteria for grounding data") | ||||||||
} | ||||||||
|
||||||||
// ALWAYS add the date and time to every prompt | ||||||||
var date = Constants.TEST_DATE | ||||||||
var time = Constants.TEST_TIME | ||||||||
if (date == "") { | ||||||||
date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) | ||||||||
} | ||||||||
if (time == "") { | ||||||||
time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm")) | ||||||||
if (messagePreamble.isNullOrEmpty()) { | ||||||||
// ONLY show date/time when embeddings are empty, as it triggers the SessionsByTime function (I THINK) | ||||||||
// ALWAYS add the date and time to every prompt | ||||||||
Comment on lines
+303
to
+304
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
hoping the "i think" has been resolved 😜 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 not sure! |
||||||||
var date = Constants.TEST_DATE | ||||||||
var time = Constants.TEST_TIME | ||||||||
if (date == "") { | ||||||||
date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) | ||||||||
} | ||||||||
if (time == "") { | ||||||||
time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm")) | ||||||||
} | ||||||||
messagePreamble = | ||||||||
"The current date is $date and the time (in 24 hour format) is $time.\n\n$messagePreamble" | ||||||||
} | ||||||||
messagePreamble = | ||||||||
"The current date is $date and the time (in 24 hour format) is $time.\n\n$messagePreamble" | ||||||||
return messagePreamble | ||||||||
} | ||||||||
|
||||||||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this still true with the custom chat message class? or can we remove the null check now