File size: 2,359 Bytes
8428236
ca52e99
07d67df
b68e86b
8428236
 
 
 
07d67df
 
bf236aa
8428236
11e06aa
 
 
07d67df
8428236
07d67df
 
 
 
 
 
8428236
b68e86b
 
 
 
8428236
 
 
bf236aa
 
11e06aa
 
 
 
 
 
 
bf236aa
11e06aa
 
 
 
 
 
 
8428236
 
 
11e06aa
 
 
 
 
 
8428236
 
 
ca52e99
 
bf236aa
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
import os
import gradio as gr
from git import Repo
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex, StorageContext, Settings
from llama_index.embeddings.openai import OpenAIEmbedding
from llama_index.llms.openai import OpenAI

# Configuração inicial
GIT_REPO = "https://github.com/luiz-ouroboros/payment_api.git"
PROJECT_DIR = "payment_api"
STORAGE_DIR = "storage"

# Configurar a chave da API da OpenAI
os.environ["OPENAI_API_KEY"] = "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"

# Baixar o repositório do GitHub (se não existir)
if not os.path.exists(PROJECT_DIR):
    print("Clonando repositório...")
    Repo.clone_from(GIT_REPO, PROJECT_DIR)

# Verificar se o repositório foi clonado corretamente
if not os.path.exists(PROJECT_DIR) or not os.listdir(PROJECT_DIR):
    raise ValueError(f"Erro: O diretório {PROJECT_DIR} está vazio ou não foi clonado corretamente.")

# Configurar LlamaIndex corretamente
Settings.llm = OpenAI()
Settings.embed_model = OpenAIEmbedding()

# Lendo os arquivos do projeto
documents = SimpleDirectoryReader(PROJECT_DIR).load_data()

# Verificar se o diretório de persistência existe
if not os.path.exists(STORAGE_DIR):
    try:
        print("Criando índice e persistindo dados...")
        index = VectorStoreIndex.from_documents(documents)
        index.storage_context.persist(persist_dir=STORAGE_DIR)
    except Exception as e:
        print(f"Erro ao criar o índice: {e}")
        raise
else:
    try:
        print("Carregando índice existente...")
        storage_context = StorageContext.from_defaults(persist_dir=STORAGE_DIR)
        index = VectorStoreIndex.from_documents(documents, storage_context=storage_context)
    except Exception as e:
        print(f"Erro ao carregar o índice: {e}")
        raise

# Função para responder perguntas sobre o projeto
def respond(message, history):
    try:
        query_engine = index.as_query_engine()
        response = query_engine.query(message)
        return response.response
    except Exception as e:
        return f"Erro ao processar a pergunta: {e}"

# Interface do chatbot no Gradio
demo = gr.ChatInterface(respond, title="IA Especializada no Meu Projeto")

if __name__ == "__main__":
    demo.launch()