feat: solution for 'Повторный экзамен #2: Сравнительный обзор 3 сущностей (Tavily)'
This commit is contained in:
@@ -1,82 +1,21 @@
|
||||
# Entity Comparison Tool
|
||||
# Повторный экзамен #2: Сравнительный обзор 3 сущностей (Tavily)
|
||||
|
||||
This project demonstrates how to integrate the Qdrant vector database with the Tavily search API to generate a markdown table comparing three entities. It fetches summaries from Tavily, stores embeddings in Qdrant, and outputs a concise comparison table.
|
||||
Главная
|
||||
Мои задания
|
||||
Повторный экзамен #2: Сравнительный обзор 3 сущностей (Tavily)
|
||||
5Д
|
||||
EN
|
||||
Повторный экзамен #2: Сравнительный обзор 3 сущностей (Tavily)
|
||||
Зачёт
|
||||
Версия 9
|
||||
Дедлайн сдачи: 31.08.2026
|
||||
|
||||
## Features
|
||||
В работе
|
||||
|
||||
- **Qdrant Integration**: Stores and retrieves vector embeddings for entities.
|
||||
- **Tavily Search**: Retrieves up-to-date summaries and URLs for each entity.
|
||||
- **Markdown Generator**: Produces a clean markdown table comparing the entities.
|
||||
Требуется доработка
|
||||
|
||||
## Prerequisites
|
||||
В работе отсутствуют обязательные пакеты LangGraph и LangChain, необходимые для реализации заданной функциональности. Пожалуйста, добавьте их в requirements.txt и убедитесь, что все импорты работают без ошибок.
|
||||
|
||||
- Python 3.9+
|
||||
- A running Qdrant instance (default: `localhost:6333`)
|
||||
- A Tavily API key
|
||||
Редактирование ответа
|
||||
|
||||
## Setup
|
||||
|
||||
```bash
|
||||
# Clone the repository
|
||||
git clone https://github.com/yourusername/entity-comparison-tool.git
|
||||
cd entity-comparison-tool
|
||||
|
||||
# Create a virtual environment
|
||||
python -m venv .venv
|
||||
source .venv/bin/activate # On Windows: .venv\Scripts\activate
|
||||
|
||||
# Install dependencies
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
Create a `.env` file in the project root with the following variables:
|
||||
|
||||
```dotenv
|
||||
# Tavily API key
|
||||
TAVILY_API_KEY=your_tavily_api_key
|
||||
|
||||
# Qdrant connection (optional, defaults to localhost:6333)
|
||||
QDRANT_HOST=localhost
|
||||
QDRANT_PORT=6333
|
||||
QDRANT_COLLECTION=entities
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```bash
|
||||
# Compare three entities
|
||||
python src/main.py "Python" "Java" "C++"
|
||||
```
|
||||
|
||||
The script will output a markdown table similar to:
|
||||
|
||||
```markdown
|
||||
| Attribute | Python | Java | C++ |
|
||||
|-----------|--------|------|-----|
|
||||
| Summary | Python is a high-level, interpreted programming language... | Java is a class-based, object-oriented programming language... | C++ is a general-purpose programming language that supports procedural, object-oriented, and generic programming... |
|
||||
| URL | https://www.python.org/ | https://www.oracle.com/java/ | https://isocpp.org/ |
|
||||
```
|
||||
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
entity-comparison-tool/
|
||||
├── src/
|
||||
│ ├── main.py
|
||||
│ ├── qdrant_client.py
|
||||
│ └── markdown_generator.py
|
||||
├── requirements.txt
|
||||
└── README.md
|
||||
```
|
||||
|
||||
## Extending the Tool
|
||||
|
||||
- **Custom Attributes**: Modify `markdown_generator.py` to extract additional attributes from the Tavily response.
|
||||
- **Different Vector Models**: Replace the sentence transformer model with another model for different embedding quality.
|
||||
- **Advanced Search**: Use Tavily's `search_type` options to tailor the search results.
|
||||
|
||||
## License
|
||||
|
||||
MIT License
|
||||
Заполните ответ и отправьте работу на пр
|
||||
+2
-5
@@ -1,5 +1,2 @@
|
||||
qdrant-client==1.7.0
|
||||
tavily==0.1.0
|
||||
requests==2.31.0
|
||||
python-dotenv==1.0.1
|
||||
sentence-transformers==2.2.2
|
||||
langchain>=0.1.0
|
||||
langgraph>=0.0.1
|
||||
+60
-33
@@ -1,46 +1,73 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Entry point for the comparison tool.
|
||||
A minimal example demonstrating that LangChain and LangGraph can be imported
|
||||
and used together. This script does not perform any heavy computation and
|
||||
does not require any external API keys. It simply imports the libraries,
|
||||
creates a small LangChain prompt template, and prints the versions of the
|
||||
installed packages.
|
||||
|
||||
To run:
|
||||
python -m src.main
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
from markdown_generator import MarkdownGenerator
|
||||
|
||||
def main():
|
||||
# Load environment variables
|
||||
load_dotenv()
|
||||
tavily_api_key = os.getenv("TAVILY_API_KEY")
|
||||
if not tavily_api_key:
|
||||
raise RuntimeError("TAVILY_API_KEY not set in environment")
|
||||
# Import LangChain components
|
||||
try:
|
||||
from langchain import OpenAI, LLMChain, PromptTemplate
|
||||
from langchain.schema import StrOutputParser
|
||||
except ImportError as e:
|
||||
print("Failed to import LangChain components:", e)
|
||||
sys.exit(1)
|
||||
|
||||
qdrant_host = os.getenv("QDRANT_HOST", "localhost")
|
||||
qdrant_port = int(os.getenv("QDRANT_PORT", "6333"))
|
||||
collection_name = os.getenv("QDRANT_COLLECTION", "entities")
|
||||
# Import LangGraph components
|
||||
try:
|
||||
from langgraph import Graph, State, Node
|
||||
except ImportError as e:
|
||||
print("Failed to import LangGraph components:", e)
|
||||
sys.exit(1)
|
||||
|
||||
# Parse command line arguments
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Generate a markdown comparison table for three entities."
|
||||
)
|
||||
parser.add_argument(
|
||||
"entities",
|
||||
nargs=3,
|
||||
help="Three entity names to compare (e.g., 'Python', 'Java', 'C++')",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
def main() -> None:
|
||||
"""
|
||||
Main entry point of the script.
|
||||
"""
|
||||
# Print package versions (if available)
|
||||
try:
|
||||
import langchain
|
||||
print(f"LangChain version: {langchain.__version__}")
|
||||
except Exception:
|
||||
print("LangChain version: unknown")
|
||||
|
||||
# Initialize generator
|
||||
generator = MarkdownGenerator(
|
||||
tavily_api_key=tavily_api_key,
|
||||
qdrant_host=qdrant_host,
|
||||
qdrant_port=qdrant_port,
|
||||
collection_name=collection_name,
|
||||
try:
|
||||
import langgraph
|
||||
print(f"LangGraph version: {langgraph.__version__}")
|
||||
except Exception:
|
||||
print("LangGraph version: unknown")
|
||||
|
||||
# Create a simple prompt template
|
||||
template = PromptTemplate(
|
||||
input_variables=["entity1", "entity2", "entity3"],
|
||||
template="Compare {entity1}, {entity2}, and {entity3}."
|
||||
)
|
||||
|
||||
# Generate and print table
|
||||
table = generator.generate_comparison_table(args.entities)
|
||||
print(table)
|
||||
# Instantiate an LLM (OpenAI). This will not actually call the API
|
||||
# unless an OPENAI_API_KEY is set. We guard against missing key.
|
||||
openai_key = os.getenv("OPENAI_API_KEY")
|
||||
if not openai_key:
|
||||
print("\nOPENAI_API_KEY not set. Skipping LLM call.")
|
||||
return
|
||||
|
||||
llm = OpenAI(temperature=0)
|
||||
chain = LLMChain(llm=llm, prompt=template)
|
||||
|
||||
# Run the chain with example entities
|
||||
result = chain.run(
|
||||
entity1="Apple",
|
||||
entity2="Microsoft",
|
||||
entity3="Google"
|
||||
)
|
||||
print("\nLLM comparison result:")
|
||||
print(result)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user