File size: 2,646 Bytes
a876cd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4dd8ab3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List
from langchain_community.vectorstores import Chroma
from langchain.prompts import ChatPromptTemplate
from get_embedding_function import get_embedding_function
from langchain_groq import ChatGroq
import chainlit as cl

app = FastAPI()

# Configurar variáveis de ambiente
os.environ["OPENAI_API_BASE"] = 'https://api.groq.com/openai/v1'
os.environ["OPENAI_MODEL_NAME"] = 'llama3-8b-8192'
os.environ["GROQ_API_KEY"] = os.getenv("GROQ_API_KEY")

CHROMA_PATH = "chroma"

PROMPT_TEMPLATE = """
You are 'Vasu', an experienced professor with extensive knowledge in Cryptocurrency, Artificial Intelligence, and related projects.
Provide relevant 'Links' "http://", but include links only when they are particularly useful for understanding the response.
Answer the question based solely on the following context: {context}

Based on the above context, answer the question: {question}.
"""

class QueryRequest(BaseModel):
    query: str

class QueryResponse(BaseModel):
    response: str
    sources: List[str]

def query_rag(query_text: str):
    # Configurar o modelo Groq
    chat_groq = ChatGroq(temperature=0, model_name="llama3-8b-8192")

    # Preparar o DB
    embedding_function = get_embedding_function()
    db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embedding_function)

    # Buscar no DB
    results = db.similarity_search_with_score(query_text, k=10)

    context_text = "\n\n---\n\n".join([doc.page_content for doc, _score in results])
    prompt_template = ChatPromptTemplate.from_template(PROMPT_TEMPLATE)
    prompt = prompt_template.format(context=context_text, question=query_text)

    # Obter a resposta usando Groq
    response_text = chat_groq.invoke(prompt).content

    sources = [doc.metadata.get("id", None) for doc, _score in results]
    return response_text, sources

@app.post("/query", response_model=QueryResponse)
async def query_api(request: QueryRequest):
    try:
        response_text, sources = query_rag(request.query)
        return QueryResponse(response=response_text, sources=sources)
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@cl.on_message
async def chainlit_main(message: cl.Message):
    query_text = message.content  # Obter a mensagem do usuário a partir do Chainlit
    response_text = query_rag(query_text)
    
    # Enviar a resposta de volta para o Chainlit
    await cl.Message(
        content=f"{response_text}",
    ).send()

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)