60 lines
2.1 KiB
Markdown
60 lines
2.1 KiB
Markdown
|
|
<<
|
|
using python, create a new project that will utilize a vector store database to create interlinked vector space knowledge graph as a "memory" function. It will be used by a realtime LLM to store and retrieve knowledge>>
|
|
#Environment Setup
|
|
cd vector_knowledge_graph
|
|
python -m venv venv
|
|
source venv/bin/activate
|
|
pip install fastapi uvicorn openai psycopg2-binary sqlalchemy
|
|
|
|
|
|
#Create a Database: Create a new PostgreSQL database.
|
|
CREATE EXTENSION vector;
|
|
CREATE TABLE knowledge (
|
|
id SERIAL PRIMARY KEY,
|
|
embedding vector(1536) NOT NULL, -- assuming 512 dimensions for embeddings; openai uses 1536
|
|
metadata JSONB
|
|
);
|
|
CREATE INDEX ON knowledge USING ivfflat (embedding);
|
|
|
|
#Application Code
|
|
<!-- FastAPI Setup (app/api/main.py): Initialize FastAPI with a simple endpoint for testing. -->
|
|
from fastapi import FastAPI
|
|
|
|
app = FastAPI()
|
|
|
|
@app.get("/")
|
|
async def read_root():
|
|
return {"Hello": "World"}
|
|
Database Client (app/vector_db/client.py): Implement a simple client for connecting to the database and inserting/fetching vectors.
|
|
|
|
python
|
|
Copy code
|
|
import psycopg2
|
|
from psycopg2.extras import Json
|
|
|
|
def insert_embedding(embedding, metadata):
|
|
conn = psycopg2.connect("dbname=your_db user=your_user")
|
|
cur = conn.cursor()
|
|
cur.execute("INSERT INTO knowledge (embedding, metadata) VALUES (%s, %s)", (embedding, Json(metadata)))
|
|
conn.commit()
|
|
cur.close()
|
|
conn.close()
|
|
|
|
def search_embedding(embedding):
|
|
conn = psycopg2.connect("dbname=your_db user=your_user")
|
|
cur = conn.cursor()
|
|
cur.execute("SELECT id, metadata FROM knowledge ORDER BY embedding <-> %s LIMIT 5", (embedding,))
|
|
results = cur.fetchall()
|
|
cur.close()
|
|
conn.close()
|
|
return results
|
|
5. LLM Integration
|
|
At this stage, we'll need to implement the logic to interact with OpenAI's API to generate and process embeddings. Since this involves using OpenAI's services, ensure you have an API key and have agreed to their terms of use.
|
|
|
|
6. Running the Application
|
|
With the basic components in place, you can start the FastAPI application using uvicorn:
|
|
|
|
bash
|
|
Copy code
|
|
uvicorn app.api.main:app --reload |