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

Add pytest-cov for test coverage and add integration test #164

Merged
merged 2 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion src/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ __pycache__
**/files/*
**/testing*
**/dist/
**/env
**/env
**/.coverage
87 changes: 86 additions & 1 deletion src/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ loguru = ">=0.7.0,<0.8.0"

[tool.poetry.group.test.dependencies]
pytest = "7.4.0"
pytest-cov = "^4.1.0"


[tool.poetry.group.lint.dependencies]
Expand Down
13 changes: 12 additions & 1 deletion src/sherpa_ai/connectors/vectorstores.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,15 @@ def get_vectordb():
cfg.CHROMA_HOST, cfg.CHROMA_PORT, cfg.CHROMA_INDEX, cfg.OPENAI_API_KEY
).as_retriever()
else:
return LocalChromaStore.from_folder("files", cfg.OPENAI_API_KEY).as_retriever()
if os.path.exists("files"):
return LocalChromaStore.from_folder(
"files", cfg.OPENAI_API_KEY
).as_retriever()
else:
logger.warning(
"No files folder found, initialize an empty vectorstore instead"
)
embedding_func = OpenAIEmbeddings(openai_api_key=cfg.OPENAI_API_KEY)
return LocalChromaStore(
"memory", embedding_function=embedding_func
).as_retriever()
67 changes: 67 additions & 0 deletions src/tests/integration_tests/test_task_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from datetime import datetime
from typing import List

import pytest
from langchain import GoogleSerperAPIWrapper
from langchain.chat_models import ChatOpenAI
from langchain.tools.base import BaseTool
from langchain.vectorstores.base import VectorStoreRetriever

import sherpa_ai.config as cfg
from sherpa_ai.connectors.vectorstores import get_vectordb
from sherpa_ai.task_agent import TaskAgent
from sherpa_ai.tools import ContextTool, SearchTool


def config_task_agent(
tools: List[BaseTool],
memory: VectorStoreRetriever,
llm=ChatOpenAI(model_name="gpt-3.5-turbo"),
) -> TaskAgent:
task_agent = TaskAgent.from_llm_and_tools(
ai_name="Sherpa",
ai_role="assistant",
ai_id="Sherpa",
memory=memory,
tools=tools,
previous_messages=[],
llm=llm,
)

return task_agent


@pytest.mark.real
def test_task_solving_with_search():
"""Test task solving with search"""
question = "What is the date today, using the following format: YYYY-MM-DD?"
date = datetime.now().strftime("%Y-%m-%d")

if cfg.SERPER_API_KEY is None:
pytest.skip(
"SERPER_API_KEY not found in environment variables, skipping this test"
)
memory = get_vectordb()
tools = [SearchTool(api_wrapper=GoogleSerperAPIWrapper())]

task_agent = config_task_agent(tools=tools, memory=memory)

response = task_agent.run(question)
assert date in response, "Today's date not found in response"


@pytest.mark.real
def test_task_solving_with_context_search():
question = "What is langchain?"

if cfg.VECTORDB != "pinecone" or cfg.VECTORDB != "chroma":
pytest.skip("VECTORDB is not configured properly, skipping this test")

memory = get_vectordb()
tools = [ContextTool(memory=memory)]

task_agent = config_task_agent(tools=tools, memory=memory)

response = task_agent.run(question)

assert "langchain" in response.lower(), "langchain not found in response"
2 changes: 1 addition & 1 deletion src/tests/unit_tests/test_error_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest
from loguru import logger

from sherpa.error_hanlding import AgentErrorHandler
from sherpa_ai.error_handling import AgentErrorHandler


class DummyLogger:
Expand Down
2 changes: 1 addition & 1 deletion src/tests/unit_tests/test_post_processor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""A unit test to test the post processor for LLM responses."""
import os

from sherpa.post_processors import md_link_to_slack
from sherpa_ai.post_processors import md_link_to_slack


def test_slack_link() -> None:
Expand Down