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

Bugs in conversations #260

Merged
merged 16 commits into from
Jul 27, 2023
Merged
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -273,7 +273,7 @@ interface Chat : LLM {
}

private fun messages(memories: List<Memory>, promptWithContext: String): List<Message> =
memories.reversed().map { it.content } +
memories.map { it.content } +
listOf(Message(Role.USER, promptWithContext, Role.USER.name))

private suspend fun memories(
Original file line number Diff line number Diff line change
@@ -31,15 +31,27 @@ private constructor(private val embeddings: Embeddings, private val state: Atomi

override suspend fun addMemories(memories: List<Memory>) {
javipacheco marked this conversation as resolved.
Show resolved Hide resolved
state.update { prevState ->
val allNewMemories = memories.groupBy { it.conversationId }

val prevConversationIdsInNewMemories = prevState.orderedMemories.map { it.key }.intersect(allNewMemories.keys)

val prevMemoriesWithNewMessages = prevState.orderedMemories.map { (conversationId, memories) ->
conversationId to (memories + allNewMemories[conversationId].orEmpty())
}.toMap()

val allNewMemoriesWithoutPrevConversationId = allNewMemories.filterKeys {
!prevConversationIdsInNewMemories.contains(it)
}

prevState.copy(
orderedMemories = prevState.orderedMemories + memories.groupBy { it.conversationId }
orderedMemories = prevMemoriesWithNewMessages + allNewMemoriesWithoutPrevConversationId
)
}
}

override suspend fun memories(conversationId: ConversationId, limit: Int): List<Memory> {
val memories = state.get().orderedMemories[conversationId]
return memories?.take(limit).orEmpty()
return memories?.takeLast(limit).orEmpty()
javipacheco marked this conversation as resolved.
Show resolved Hide resolved
}

override suspend fun addTexts(texts: List<String>) {
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.xebia.functional.xef.vectorstores

import com.xebia.functional.xef.embeddings.Embedding
import com.xebia.functional.xef.embeddings.Embeddings
import com.xebia.functional.xef.llm.models.embeddings.RequestConfig

class FakeEmbeddings : Embeddings {
override suspend fun embedDocuments(
texts: List<String>, chunkSize: Int?, requestConfig: RequestConfig
): List<Embedding> = emptyList()


override suspend fun embedQuery(text: String, requestConfig: RequestConfig): List<Embedding> = emptyList()
}
javipacheco marked this conversation as resolved.
Show resolved Hide resolved
raulraja marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.xebia.functional.xef.vectorstores

import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe

class LocalVectorStoreSpec :
StringSpec({
"memories function should return all of messages in the right order when the limit is greater than the number of stored messages" {
val localVectorStore = LocalVectorStore(FakeEmbeddings())

localVectorStore.addMemories(messages1)
localVectorStore.addMemories(messages2)

val messages = localVectorStore.memories(defaultConversationId, Int.MAX_VALUE)

val messagesExpected = messages1 + messages2

messages shouldBe messagesExpected
}

"memories function should return the last n messages in the right order" {
val localVectorStore = LocalVectorStore(FakeEmbeddings())

localVectorStore.addMemories(messages1)
localVectorStore.addMemories(messages2)

val messages = localVectorStore.memories(defaultConversationId, 2)
javipacheco marked this conversation as resolved.
Show resolved Hide resolved

val messagesExpected = (messages1 + messages2).takeLast(2)

messages shouldBe messagesExpected
}

})
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.xebia.functional.xef.vectorstores

import com.xebia.functional.xef.llm.models.chat.Message
import com.xebia.functional.xef.llm.models.chat.Role

val defaultConversationId = ConversationId("default-id")

val messages1 = listOf(
Memory(defaultConversationId, Message(Role.USER, "Who is the best player in the world?", "USER"), 0),
Memory(defaultConversationId, Message(Role.ASSISTANT, "Magico Gonzalez", "ASSISTANT"), 0),
)

val messages2 = listOf(
Memory(defaultConversationId, Message(Role.USER, "Which is the most beautiful city in the world?", "USER"), 0),
Memory(defaultConversationId, Message(Role.ASSISTANT, "More than a city better an area, La Bahia de Cadiz", "ASSISTANT"), 0),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.xebia.functional.xef.auto

import com.xebia.functional.xef.auto.llm.openai.getOrElse
import com.xebia.functional.xef.auto.llm.openai.promptMessage

suspend fun main() {
ai {

val email: String =
promptMessage(
"""
|You are a Marketing Responsible and have the information about different products. You have to prepare
|an email template with the personal information
""".trimMargin()
)
println("First question:\n $email")
javipacheco marked this conversation as resolved.
Show resolved Hide resolved

val summarize: String =
promptMessage(
"""
|You are a Marketing Responsible and have the information about the best rated products.
|Summarize the next information:
|Love this product and so does my husband! He tried it because his face gets chapped and red from
|working outside. It actually helped by about 60%! I love it cuz it's lightweight and smells so yummy!
|After applying makeup, it doesn't leave streaks like other moisturizers cause. i would definitely use
|this!
|
|I've been using this for 10+yrs now. I don't have any noticeable
|wrinkles at all. I use Estée Lauder's micro essence then advance repair serum before I apply this
|lotion. A little goes a long way! It does feel greasier than most face lotions that I've tried
|previously but I don't apply much. I enjoy cucumber like scent of the face lotion. I have combination
|skin and never broke out using this. This is my daily skincare product with or without makeup. And it
|has SPF but I also apply Kravebeauty SPF on top as well for extra protection
""".trimMargin()
)
println("Second question:\n $summarize")

val meaning: String =
promptMessage(
"""
|What is the meaning of life?
""".trimMargin()
)
println("Third question:\n $meaning")


}.getOrElse { println(it) }
}