quivr/backend/core/tests/conftest.py
AmineDiro 35eb07f7a2
fix: quiv core stream duplicate and quivr-core rag tests (#2852)
# Description

`quivr-core`
- Generate a fixture to simulate a model with function calling
- Monkey patch `QuivrQARAG` stream 
-  Tests function

`quivr-api`
- Fixes empty API responses 
- Fixes non function calling models

---------

Co-authored-by: Stan Girard <girard.stanislas@gmail.com>
2024-07-12 06:07:39 -07:00

66 lines
2.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import json
import os
import pytest
from langchain_core.embeddings import DeterministicFakeEmbedding
from langchain_core.language_models import FakeListChatModel
from langchain_core.messages.ai import AIMessageChunk
from langchain_core.runnables.utils import AddableDict
from langchain_core.vectorstores import InMemoryVectorStore
from quivr_core.config import LLMEndpointConfig
from quivr_core.llm import LLMEndpoint
@pytest.fixture
def full_response():
return "Natural Language Processing (NLP) is a field of artificial intelligence that focuses on the interaction between computers and humans through natural language. The ultimate objective of NLP is to enable computers to understand, interpret, and respond to human language in a way that is both valuable and meaningful. NLP combines computational linguistics—rule-based modeling of human language—with statistical, machine learning, and deep learning models. This combination allows computers to process human language in the form of text or voice data and to understand its full meaning, complete with the speaker or writers intent and sentiment. Key tasks in NLP include text and speech recognition, translation, sentiment analysis, and topic segmentation."
@pytest.fixture
def chunks_stream_answer():
with open("./tests/chunk_stream_fixture.jsonl", "r") as f:
raw_chunks = list(f)
chunks = []
for rc in raw_chunks:
chunk = AddableDict(**json.loads(rc))
if "answer" in chunk:
chunk["answer"] = AIMessageChunk(**chunk["answer"])
chunks.append(chunk)
return chunks
@pytest.fixture(autouse=True)
def openai_api_key():
os.environ["OPENAI_API_KEY"] = "abcd"
@pytest.fixture(scope="function")
def temp_data_file(tmp_path):
data = "This is some test data."
temp_file = tmp_path / "data.txt"
temp_file.write_text(data)
return temp_file
@pytest.fixture
def answers():
return [f"answer_{i}" for i in range(10)]
@pytest.fixture(scope="function")
def fake_llm(answers: list[str]):
llm = FakeListChatModel(responses=answers)
return LLMEndpoint(llm=llm, llm_config=LLMEndpointConfig(model="fake_model"))
@pytest.fixture(scope="function")
def embedder():
return DeterministicFakeEmbedding(size=20)
@pytest.fixture(scope="function")
def mem_vector_store(embedder):
return InMemoryVectorStore(embedder)