-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(api): search vectorstore using only last message (#939)
* Search VectorStore using only last message * tests: add assertion for correct RAG results
- Loading branch information
Showing
16 changed files
with
126 additions
and
36 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
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Cats are fascinating creatures known for their agility and independence. Domestic cats, descendants of African wildcats, have been companions to humans for over 4,000 years. They possess excellent night vision, allowing them to see in light levels six times lower than what humans need. |
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 @@ | ||
With approximately 32 muscles in each ear, cats have an exceptional sense of hearing and can rotate their ears 180 degrees. Their unique grooming behavior helps to maintain their coat and regulate body temperature. Additionally, cats have a specialized collarbone that enables them to always land on their feet, a skill they demonstrate with remarkable precision. |
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 @@ | ||
Cats are remarkable animals with a range of intriguing characteristics. They have a highly developed sense of smell, with about 50 to 80 million scent receptors in their noses, compared to humans who have only around 5 million. |
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 @@ | ||
Cats can make over 100 different sounds, including purring, meowing, and hissing, which they use to communicate with their humans and other animals. Their whiskers are highly sensitive and can detect changes in their environment, helping them navigate and judge spaces. |
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 @@ | ||
Cats also have a unique grooming behavior, with their tongues covered in tiny, hook-shaped structures called papillae that help them clean their fur and remove loose hair. Additionally, cats sleep for about 12 to 16 hours a day, making them one of the sleepiest of all domestic animals. |
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 @@ | ||
There is only one piece of fruit in the fridge. It is an orange, and located on the bottom shelf, behind the pot of stew. |
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,77 @@ | ||
import os | ||
from pathlib import Path | ||
from openai.types.beta.threads.text import Text | ||
|
||
from ...utils.client import client_config_factory | ||
|
||
|
||
def make_test_assistant(client, model, vector_store_id): | ||
assistant = client.beta.assistants.create( | ||
name="Test Assistant", | ||
instructions="You must provide a response based on the attached files.", | ||
model=model, | ||
tools=[{"type": "file_search"}], | ||
tool_resources={"file_search": {"vector_store_ids": [vector_store_id]}}, | ||
) | ||
return assistant | ||
|
||
|
||
def make_test_run(client, assistant, thread): | ||
run = client.beta.threads.runs.create_and_poll( | ||
assistant_id=assistant.id, thread_id=thread.id | ||
) | ||
return run | ||
|
||
|
||
def test_rag_needle_haystack(): | ||
config = client_config_factory("leapfrogai") | ||
client = config.client | ||
|
||
vector_store = client.beta.vector_stores.create(name="Test data") | ||
file_path = "../../data" | ||
file_names = [ | ||
"test_rag_1.1.txt", | ||
"test_rag_1.2.txt", | ||
"test_rag_1.3.txt", | ||
"test_rag_1.4.txt", | ||
"test_rag_1.5.txt", | ||
"test_rag_2.1.txt", | ||
] | ||
vector_store_files = [] | ||
for file_name in file_names: | ||
with open( | ||
f"{Path(os.path.dirname(__file__))}/{file_path}/{file_name}", "rb" | ||
) as file: | ||
vector_store_files.append( | ||
client.beta.vector_stores.files.upload( | ||
vector_store_id=vector_store.id, file=file | ||
) | ||
) | ||
|
||
assistant = make_test_assistant(client, config.model, vector_store.id) | ||
thread = client.beta.threads.create() | ||
|
||
client.beta.threads.messages.create( | ||
thread_id=thread.id, | ||
role="user", | ||
content="Tell me about cats.", | ||
) | ||
client.beta.threads.messages.create( | ||
thread_id=thread.id, | ||
role="user", | ||
content="There is one piece of fruit in the fridge. What is it and where is it located?", | ||
) | ||
run = make_test_run(client, assistant, thread) | ||
|
||
messages = client.beta.threads.messages.list( | ||
thread_id=thread.id, run_id=run.id | ||
).data | ||
|
||
# Get the response content from the last message | ||
message_content = messages[-1].content[0].text | ||
assert isinstance(message_content, Text) | ||
assert "orange" in message_content.value | ||
assert len(message_content.annotations) > 0 | ||
|
||
for a in message_content.annotations: | ||
print(a.text) |
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