johannesocean / pgvector-demo

19 stars 4 forks source link

An error running add_test_data.py #2

Open Vladibik opened 4 months ago

Vladibik commented 4 months ago

https://prnt.sc/ZEU0MxY2UTnn

edjung commented 2 months ago

You need to add export PYTHONPATH=. After that there are additional packages that may need to be installed: matplotlib, plotly, scikit-learn It then runs but it seems that the demo code assumes you have an openAI key and your environment is already set up for it. After adding an embedding model: export EMBEDDING_MODEL=text-embedding-ada-002

  File "/home/ed/.local/lib/python3.10/site-packages/openai/util.py", line 186, in default_api_key
    raise openai.error.AuthenticationError(
openai.error.AuthenticationError: No API key provided. You can set your API key in code using 'openai.api_key = <API-KEY>', or you can set the environment variable OPENAI_API_KEY=<API-KEY>). If your API key is stored in a file, you can point the openai module at it with 'openai.api_key_path = <PATH>'. You can generate API keys in the OpenAI web interface. See https://platform.openai.com/account/api-keys for details.

It's fairly easy to swap out the openAI stuff with Google Gemini which can be used for free:

import google.generativeai as genai

API_KEY = os.environ['API_KEY']
genai.configure(api_key=API_KEY)
    embeddings = []
    for t in texts:
        e = genai.embed_content(model='models/embedding-001', content=t, task_type="semantic_similarity")
        embeddings.append(e['embedding'])
➜  pgvector-demo git:(main) ✗ python3 app/db/data/gadd_test_data.py
➜  pgvector-demo git:(main) ✗ python3 app/gtest_search.py
Text: My sister adopted a kitten yesterday.; Similarity: 0.8415076940655926
Text: Chinchillas and kittens are cute.; Similarity: 0.7240812222316265
Text: Look at this cute hamster munching on a piece of broccoli.; Similarity: 0.6112853391418669
johannesocean commented 2 months ago

@Vladibik: @edjung is correct, you need to add the project path to your python environment. I usually use PyCharm which handles that stuff for me.

one workaround could be to add the following code at the top of the add_test_data.py file:

import sys
from pathlib import Path

current_file = Path(__file__)
project_root = current_file.parents[3]
sys.path.append(str(project_root))

from app import BASE_DIR  # simply because we need to load .env