diff --git a/chroma.sqlite3 b/chroma.sqlite3 new file mode 100644 index 000000000..8b32ac1c4 Binary files /dev/null and b/chroma.sqlite3 differ diff --git a/examples/use-rdb-resource/README.md b/examples/use-rdb-resource/README.md index e073b6238..d8bdd515f 100644 --- a/examples/use-rdb-resource/README.md +++ b/examples/use-rdb-resource/README.md @@ -22,3 +22,5 @@ - `poetry run python make_example_table_data.py` - if this command doesn't work, then run `poetry env use 3.12` - change python version to resolve dependensies version + - `poetry run python main.py` + - run main file to answer question by DANA using DbResource, and see the result in the terminal. diff --git a/examples/use-rdb-resource/main.py b/examples/use-rdb-resource/main.py index 81ab173e1..b1ec3151f 100644 --- a/examples/use-rdb-resource/main.py +++ b/examples/use-rdb-resource/main.py @@ -24,6 +24,7 @@ def solve(question, query) -> str: ) query = generate_sql_from_prompt(QUESTION) + print(query) answer = solve(QUESTION, query) print('--------------------------------') diff --git a/examples/use-rdb-resource/pyproject.toml b/examples/use-rdb-resource/pyproject.toml index cd5c4683d..58c170130 100644 --- a/examples/use-rdb-resource/pyproject.toml +++ b/examples/use-rdb-resource/pyproject.toml @@ -15,6 +15,7 @@ faker = "^30.1.0" sqlalchemy = "^2.0.35" openai = "^1.51.2" openssa = "^0.24.10.10" +plotly = "^5.24.1" [build-system] diff --git a/openssa/core/resource/db.py b/openssa/core/resource/db.py index 182b0c3e2..3b26bc38f 100644 --- a/openssa/core/resource/db.py +++ b/openssa/core/resource/db.py @@ -14,6 +14,7 @@ import os from sqlalchemy import create_engine, text from sqlalchemy.orm import sessionmaker +from llama_index.core import SummaryIndex, Document from .abstract import AbstractResource from ._global import global_register @@ -70,11 +71,15 @@ def fetch_data(self) -> list[tuple[Any]]: def answer(self, question: str, n_words: int = 1000) -> str: """Answer question from database-stored Informational Resource.""" data = self.fetch_data() - # Here you can implement a more sophisticated way to generate answers from the data - # For simplicity, we will just return the fetched data as a string - return str(data) - - def __del__(self): - """Ensure the database connection is closed when the object is deleted.""" - if hasattr(self, 'db'): - self.db.Session.close_all() + print(data) + # データベースから取得したデータをドキュメントに変換 + documents = [Document(text=str(row[0]), metadata={'id': row[1]}) for row in data] + # print(documents) + index = SummaryIndex.from_documents(documents) + # print(index) + # set Logging to DEBUG for more detailed outputs + query_engine = index.as_query_engine() + # print(query_engine) + response = query_engine.query(question) + # print(response) + return response diff --git a/tests/core/resource/test_db_resource.py b/tests/core/resource/test_db_resource.py index a508ea191..d965f9c0e 100644 --- a/tests/core/resource/test_db_resource.py +++ b/tests/core/resource/test_db_resource.py @@ -3,16 +3,23 @@ load_dotenv() +#TODO: Fix hallucination +# Given Data: [(1, 'Laptop', 100000), (2, 'Smartphone', 60000), (3, 'Headphones', 8000), (4, 'Keyboard', 3000), (5, 'Mouse', 2000), (6, 'Monitor', 25000), (7, 'Tablet', 50000), (8, 'Smartwatch', 20000), (9, 'Camera', 45000), (10, 'Speaker', 15000)] +# Answer: The item that is the most expensive from the given data is the Camera. +# The Answer Should be: Laptop(100000). +# How to fix it? Make query by vanna or change the process in llama_index? def test_db_resource(): - test_url = "http://paulgraham.com/worked.html" - test_question = "What did the author do growing up?" + test_query = "SELECT * FROM items" + test_question = "Which item is the most expensive from given data?" - webpage1 = WebPageResource(test_url) - # print(f"unique name = {webpage1.name}") - # print(f"unique name = {webpage1.unique_name}") - # print(f"answer = {webpage1.answer(test_question)}") - # print(f"summary = {webpage1.get_summary()}") - _ = webpage1.answer(test_question) - _ = webpage1.get_summary() + rdb1 = DbResource(query=test_query) + # print(f"unique name = {rdb1.name}") + # print(f"unique name = {rdb1.unique_name}") + # print(f"answer = {rdb1.answer(test_question)}") + # print(f"summary = {rdb1.get_summary()}") + _ = rdb1.answer(test_question) # assert isinstance(answer, str) + print(_) + +test_db_resource()