Spaces:
Runtime error
Runtime error
Add guest stories and auto-build Chroma database
Browse files- data/guest_stories/ada_lovelace.md +7 -0
- data/guest_stories/alan_turning.md +7 -0
- data/guest_stories/grace_hopper.md +7 -0
- requirements.txt +5 -1
- tools/rag_tool.py +52 -16
data/guest_stories/ada_lovelace.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Ada Lovelace
|
| 2 |
+
|
| 3 |
+
Ada Lovelace is considered the world's first computer programmer.
|
| 4 |
+
|
| 5 |
+
She collaborated with Charles Babbage on the Analytical Engine.
|
| 6 |
+
|
| 7 |
+
At the AI Gala she will speak about creativity, mathematics, and artificial intelligence.
|
data/guest_stories/alan_turning.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Alan Turing
|
| 2 |
+
|
| 3 |
+
Alan Turing is known as the father of theoretical computer science.
|
| 4 |
+
|
| 5 |
+
He helped break the Enigma code during World War II.
|
| 6 |
+
|
| 7 |
+
At the AI Gala he will discuss machine intelligence and the Turing Test.
|
data/guest_stories/grace_hopper.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Grace Hopper
|
| 2 |
+
|
| 3 |
+
Grace Hopper developed one of the first compilers.
|
| 4 |
+
|
| 5 |
+
She popularized machine-independent programming languages.
|
| 6 |
+
|
| 7 |
+
At the AI Gala she will speak about programming languages and software engineering.
|
requirements.txt
CHANGED
|
@@ -6,4 +6,8 @@ llama-index
|
|
| 6 |
llama-index-vector-stores-chroma
|
| 7 |
llama-index-embeddings-huggingface
|
| 8 |
sentence-transformers
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
llama-index-vector-stores-chroma
|
| 7 |
llama-index-embeddings-huggingface
|
| 8 |
sentence-transformers
|
| 9 |
+
huggingface-hub
|
| 10 |
+
openai
|
| 11 |
+
gradio
|
| 12 |
+
pandas
|
| 13 |
+
requests
|
tools/rag_tool.py
CHANGED
|
@@ -1,37 +1,79 @@
|
|
|
|
|
| 1 |
import chromadb
|
| 2 |
|
| 3 |
from smolagents import tool
|
| 4 |
|
| 5 |
-
from llama_index.core import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 7 |
from llama_index.vector_stores.chroma import ChromaVectorStore
|
| 8 |
|
| 9 |
|
| 10 |
-
#
|
|
|
|
|
|
|
| 11 |
embed_model = HuggingFaceEmbedding(
|
| 12 |
model_name="BAAI/bge-small-en-v1.5"
|
| 13 |
)
|
| 14 |
|
| 15 |
-
#
|
|
|
|
|
|
|
| 16 |
db = chromadb.PersistentClient(path="./chroma_db")
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
vector_store = ChromaVectorStore(
|
| 21 |
chroma_collection=collection
|
| 22 |
)
|
| 23 |
|
| 24 |
-
storage_context = StorageContext.from_defaults(
|
| 25 |
-
vector_store=vector_store
|
| 26 |
-
)
|
| 27 |
-
|
| 28 |
-
# Load the index from the vector store
|
| 29 |
index = VectorStoreIndex.from_vector_store(
|
| 30 |
vector_store=vector_store,
|
| 31 |
embed_model=embed_model,
|
| 32 |
)
|
| 33 |
|
| 34 |
-
# Create the retriever
|
| 35 |
retriever = index.as_retriever(similarity_top_k=1)
|
| 36 |
|
| 37 |
|
|
@@ -53,12 +95,6 @@ def search_guest_story(query: str) -> str:
|
|
| 53 |
- event planning
|
| 54 |
- laws or regulations
|
| 55 |
- general internet searches
|
| 56 |
-
|
| 57 |
-
Args:
|
| 58 |
-
query: A question about an AI Gala guest.
|
| 59 |
-
|
| 60 |
-
Returns:
|
| 61 |
-
Relevant guest story text.
|
| 62 |
"""
|
| 63 |
|
| 64 |
results = retriever.retrieve(query)
|
|
|
|
| 1 |
+
import os
|
| 2 |
import chromadb
|
| 3 |
|
| 4 |
from smolagents import tool
|
| 5 |
|
| 6 |
+
from llama_index.core import (
|
| 7 |
+
StorageContext,
|
| 8 |
+
VectorStoreIndex,
|
| 9 |
+
SimpleDirectoryReader,
|
| 10 |
+
)
|
| 11 |
+
from llama_index.core.node_parser import SentenceSplitter
|
| 12 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 13 |
from llama_index.vector_stores.chroma import ChromaVectorStore
|
| 14 |
|
| 15 |
|
| 16 |
+
# -----------------------------
|
| 17 |
+
# Embedding model
|
| 18 |
+
# -----------------------------
|
| 19 |
embed_model = HuggingFaceEmbedding(
|
| 20 |
model_name="BAAI/bge-small-en-v1.5"
|
| 21 |
)
|
| 22 |
|
| 23 |
+
# -----------------------------
|
| 24 |
+
# Chroma DB
|
| 25 |
+
# -----------------------------
|
| 26 |
db = chromadb.PersistentClient(path="./chroma_db")
|
| 27 |
|
| 28 |
+
# -----------------------------
|
| 29 |
+
# Create collection if needed
|
| 30 |
+
# -----------------------------
|
| 31 |
+
try:
|
| 32 |
+
collection = db.get_collection("guest_stories")
|
| 33 |
+
except Exception:
|
| 34 |
+
print("Chroma DB not found. Creating it...")
|
| 35 |
+
|
| 36 |
+
documents = SimpleDirectoryReader(
|
| 37 |
+
"data/guest_stories"
|
| 38 |
+
).load_data()
|
| 39 |
+
|
| 40 |
+
splitter = SentenceSplitter(
|
| 41 |
+
chunk_size=512,
|
| 42 |
+
chunk_overlap=50,
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
nodes = splitter.get_nodes_from_documents(documents)
|
| 46 |
+
|
| 47 |
+
collection = db.get_or_create_collection("guest_stories")
|
| 48 |
+
|
| 49 |
+
vector_store = ChromaVectorStore(
|
| 50 |
+
chroma_collection=collection
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
storage_context = StorageContext.from_defaults(
|
| 54 |
+
vector_store=vector_store
|
| 55 |
+
)
|
| 56 |
|
| 57 |
+
VectorStoreIndex(
|
| 58 |
+
nodes,
|
| 59 |
+
storage_context=storage_context,
|
| 60 |
+
embed_model=embed_model,
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
print("Chroma DB created successfully.")
|
| 64 |
+
|
| 65 |
+
# -----------------------------
|
| 66 |
+
# Load vector store
|
| 67 |
+
# -----------------------------
|
| 68 |
vector_store = ChromaVectorStore(
|
| 69 |
chroma_collection=collection
|
| 70 |
)
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
index = VectorStoreIndex.from_vector_store(
|
| 73 |
vector_store=vector_store,
|
| 74 |
embed_model=embed_model,
|
| 75 |
)
|
| 76 |
|
|
|
|
| 77 |
retriever = index.as_retriever(similarity_top_k=1)
|
| 78 |
|
| 79 |
|
|
|
|
| 95 |
- event planning
|
| 96 |
- laws or regulations
|
| 97 |
- general internet searches
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
"""
|
| 99 |
|
| 100 |
results = retriever.retrieve(query)
|