Spaces:
Runtime error
Runtime error
Victor2323
commited on
Commit
•
7e0035d
1
Parent(s):
6c49815
Upload 9 files
Browse files- .gitattributes +1 -0
- Dockerfile +13 -0
- app.py +61 -0
- chroma/45ca7996-a391-40cb-a525-0676e4c9d12b/data_level0.bin +3 -0
- chroma/45ca7996-a391-40cb-a525-0676e4c9d12b/header.bin +3 -0
- chroma/45ca7996-a391-40cb-a525-0676e4c9d12b/length.bin +3 -0
- chroma/45ca7996-a391-40cb-a525-0676e4c9d12b/link_lists.bin +3 -0
- chroma/chroma.sqlite3 +3 -0
- get_embedding_function.py +6 -0
- requirements.txt +6 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
chroma/chroma.sqlite3 filter=lfs diff=lfs merge=lfs -text
|
Dockerfile
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.12.4
|
2 |
+
|
3 |
+
RUN useradd -m -u 1000 user
|
4 |
+
USER user
|
5 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
6 |
+
|
7 |
+
WORKDIR /app
|
8 |
+
|
9 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
10 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
11 |
+
|
12 |
+
COPY --chown=user . /app
|
13 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from fastapi import FastAPI, HTTPException
|
3 |
+
from pydantic import BaseModel
|
4 |
+
from typing import List
|
5 |
+
from langchain_community.vectorstores import Chroma
|
6 |
+
from langchain.prompts import ChatPromptTemplate
|
7 |
+
from get_embedding_function import get_embedding_function
|
8 |
+
from langchain_groq import ChatGroq
|
9 |
+
|
10 |
+
app = FastAPI()
|
11 |
+
|
12 |
+
# Configurar variáveis de ambiente
|
13 |
+
os.environ["OPENAI_API_BASE"] = 'https://api.groq.com/openai/v1'
|
14 |
+
os.environ["OPENAI_MODEL_NAME"] = 'llama3-8b-8192'
|
15 |
+
os.environ["GROQ_API_KEY"] = os.getenv("GROQ_API_KEY")
|
16 |
+
|
17 |
+
CHROMA_PATH = "chroma"
|
18 |
+
|
19 |
+
PROMPT_TEMPLATE = """
|
20 |
+
You are 'Vasu', an experienced professor with extensive knowledge in Cryptocurrency, Artificial Intelligence, and related projects.
|
21 |
+
Provide relevant 'Links' "http://", but include links only when they are particularly useful for understanding the response.
|
22 |
+
Answer the question based solely on the following context: {context}
|
23 |
+
|
24 |
+
Based on the above context, answer the question: {question}.
|
25 |
+
"""
|
26 |
+
|
27 |
+
class QueryRequest(BaseModel):
|
28 |
+
query: str
|
29 |
+
|
30 |
+
class QueryResponse(BaseModel):
|
31 |
+
response: str
|
32 |
+
sources: List[str]
|
33 |
+
|
34 |
+
def query_rag(query_text: str):
|
35 |
+
# Configurar o modelo Groq
|
36 |
+
chat_groq = ChatGroq(temperature=0, model_name="llama3-8b-8192")
|
37 |
+
|
38 |
+
# Preparar o DB
|
39 |
+
embedding_function = get_embedding_function()
|
40 |
+
db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embedding_function)
|
41 |
+
|
42 |
+
# Buscar no DB
|
43 |
+
results = db.similarity_search_with_score(query_text, k=10)
|
44 |
+
|
45 |
+
context_text = "\n\n---\n\n".join([doc.page_content for doc, _score in results])
|
46 |
+
prompt_template = ChatPromptTemplate.from_template(PROMPT_TEMPLATE)
|
47 |
+
prompt = prompt_template.format(context=context_text, question=query_text)
|
48 |
+
|
49 |
+
# Obter a resposta usando Groq
|
50 |
+
response_text = chat_groq.invoke(prompt).content
|
51 |
+
|
52 |
+
sources = [doc.metadata.get("id", None) for doc, _score in results]
|
53 |
+
return response_text, sources
|
54 |
+
|
55 |
+
@app.post("/query", response_model=QueryResponse)
|
56 |
+
async def query_api(request: QueryRequest):
|
57 |
+
try:
|
58 |
+
response_text, sources = query_rag(request.query)
|
59 |
+
return QueryResponse(response=response_text, sources=sources)
|
60 |
+
except Exception as e:
|
61 |
+
raise HTTPException(status_code=500, detail=str(e))
|
chroma/45ca7996-a391-40cb-a525-0676e4c9d12b/data_level0.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:95042e844cfb77b20e578cf65635282a99d7c4dd20e589ac062f38bc389f8e58
|
3 |
+
size 4236000
|
chroma/45ca7996-a391-40cb-a525-0676e4c9d12b/header.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:fcc596bc1909f7cc610d5839236c90513b4fbad06776c253fa1b21bfd712e940
|
3 |
+
size 100
|
chroma/45ca7996-a391-40cb-a525-0676e4c9d12b/length.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:fc19b1997119425765295aeab72d76faa6927d4f83985d328c26f20468d6cc76
|
3 |
+
size 4000
|
chroma/45ca7996-a391-40cb-a525-0676e4c9d12b/link_lists.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
|
3 |
+
size 0
|
chroma/chroma.sqlite3
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:62e5a3345fbe263e9af31b3a3e57215dffa1475129a9762da20c7a9f85bfe155
|
3 |
+
size 10698752
|
get_embedding_function.py
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain_community.embeddings.ollama import OllamaEmbeddings
|
2 |
+
|
3 |
+
def get_embedding_function():
|
4 |
+
embeddings = OllamaEmbeddings(model="mxbai-embed-large",
|
5 |
+
show_progress=True)
|
6 |
+
return embeddings
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn[standard]
|
3 |
+
langchain_community
|
4 |
+
langchain
|
5 |
+
chainlit
|
6 |
+
python-dotenv
|