add chat with memory tutorial
This commit is contained in:
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import os
|
||||
from openai import OpenAI
|
||||
|
||||
def call_llm(messages):
|
||||
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", "your-api-key"))
|
||||
|
||||
response = client.chat.completions.create(
|
||||
model="gpt-4o",
|
||||
messages=messages,
|
||||
temperature=0.7
|
||||
)
|
||||
|
||||
return response.choices[0].message.content
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Test the LLM call
|
||||
messages = [{"role": "user", "content": "In a few words, what's the meaning of life?"}]
|
||||
response = call_llm(messages)
|
||||
print(f"Prompt: {messages[0]['content']}")
|
||||
print(f"Response: {response}")
|
||||
@@ -0,0 +1,33 @@
|
||||
import os
|
||||
import numpy as np
|
||||
from openai import OpenAI
|
||||
|
||||
def get_embedding(text):
|
||||
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", "YOUR_API_KEY"))
|
||||
|
||||
response = client.embeddings.create(
|
||||
model="text-embedding-ada-002",
|
||||
input=text
|
||||
)
|
||||
|
||||
# Extract the embedding vector from the response
|
||||
embedding = response.data[0].embedding
|
||||
|
||||
# Convert to numpy array for consistency with other embedding functions
|
||||
return np.array(embedding, dtype=np.float32)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Test the embedding function
|
||||
text1 = "The quick brown fox jumps over the lazy dog."
|
||||
text2 = "Python is a popular programming language for data science."
|
||||
|
||||
emb1 = get_embedding(text1)
|
||||
emb2 = get_embedding(text2)
|
||||
|
||||
print(f"Embedding 1 shape: {emb1.shape}")
|
||||
print(f"Embedding 2 shape: {emb2.shape}")
|
||||
|
||||
# Calculate similarity (dot product)
|
||||
similarity = np.dot(emb1, emb2)
|
||||
print(f"Similarity between texts: {similarity:.4f}")
|
||||
@@ -0,0 +1,65 @@
|
||||
import numpy as np
|
||||
import faiss
|
||||
|
||||
def create_index(dimension=1536):
|
||||
return faiss.IndexFlatL2(dimension)
|
||||
|
||||
def add_vector(index, vector):
|
||||
# Make sure the vector is a numpy array with the right shape for FAISS
|
||||
vector = np.array(vector).reshape(1, -1).astype(np.float32)
|
||||
|
||||
# Add the vector to the index
|
||||
index.add(vector)
|
||||
|
||||
# Return the position (index.ntotal is the total number of vectors in the index)
|
||||
return index.ntotal - 1
|
||||
|
||||
def search_vectors(index, query_vector, k=1):
|
||||
"""Search for the k most similar vectors to the query vector
|
||||
|
||||
Args:
|
||||
index: The FAISS index
|
||||
query_vector: The query vector (numpy array or list)
|
||||
k: Number of results to return (default: 1)
|
||||
|
||||
Returns:
|
||||
tuple: (indices, distances) where:
|
||||
- indices is a list of positions in the index
|
||||
- distances is a list of the corresponding distances
|
||||
"""
|
||||
# Make sure we don't try to retrieve more vectors than exist in the index
|
||||
k = min(k, index.ntotal)
|
||||
if k == 0:
|
||||
return [], []
|
||||
|
||||
# Make sure the query is a numpy array with the right shape for FAISS
|
||||
query_vector = np.array(query_vector).reshape(1, -1).astype(np.float32)
|
||||
|
||||
# Search the index
|
||||
distances, indices = index.search(query_vector, k)
|
||||
|
||||
return indices[0].tolist(), distances[0].tolist()
|
||||
|
||||
# Example usage
|
||||
if __name__ == "__main__":
|
||||
# Create a new index
|
||||
index = create_index(dimension=3)
|
||||
|
||||
# Add some random vectors and track them separately
|
||||
items = []
|
||||
for i in range(5):
|
||||
vector = np.random.random(3)
|
||||
position = add_vector(index, vector)
|
||||
items.append(f"Item {i}")
|
||||
print(f"Added vector at position {position}")
|
||||
|
||||
print(f"Index contains {index.ntotal} vectors")
|
||||
|
||||
# Search for a similar vector
|
||||
query = np.random.random(3)
|
||||
indices, distances = search_vectors(index, query, k=2)
|
||||
|
||||
print("Query:", query)
|
||||
print("Found indices:", indices)
|
||||
print("Distances:", distances)
|
||||
print("Retrieved items:", [items[idx] for idx in indices])
|
||||
Reference in New Issue
Block a user