Skip to content

Commit

Permalink
save tmp (need to be fixed)
Browse files Browse the repository at this point in the history
  • Loading branch information
aran-nakayama committed Oct 22, 2024
1 parent 27d9c37 commit 39b218b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 17 deletions.
Binary file added chroma.sqlite3
Binary file not shown.
2 changes: 2 additions & 0 deletions examples/use-rdb-resource/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Check notice on line 26 in examples/use-rdb-resource/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

examples/use-rdb-resource/README.md#L26

Expected: 80; Actual: 100
1 change: 1 addition & 0 deletions examples/use-rdb-resource/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def solve(question, query) -> str:
)

query = generate_sql_from_prompt(QUESTION)
print(query)
answer = solve(QUESTION, query)

print('--------------------------------')
Expand Down
1 change: 1 addition & 0 deletions examples/use-rdb-resource/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
21 changes: 13 additions & 8 deletions openssa/core/resource/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
25 changes: 16 additions & 9 deletions tests/core/resource/test_db_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 39b218b

Please sign in to comment.