luansouza4444 commited on
Commit
58e44b7
·
verified ·
1 Parent(s): 3e1c619

Upload 2 files

Browse files
Files changed (2) hide show
  1. .env.txt +1 -0
  2. chatbot.py +62 -0
.env.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ OPENROUTER_API_KEY=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
chatbot.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """chatbot
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1ZBbgzg8TvtDp13fmfvJolHegnlAiN70C
8
+ """
9
+
10
+ OPENROUTER_API_KEY= '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'
11
+
12
+ !pip install python-dotenv
13
+
14
+ !pip install langchain-community --quiet
15
+
16
+ !pip install -U langchain langchain-community --quiet
17
+
18
+ !pip install gradio --quiet
19
+
20
+ !pip install -U langchain-openai --quiet
21
+
22
+ import os
23
+ from dotenv import load_dotenv
24
+ from langchain_openai import ChatOpenAI
25
+ import gradio as gr
26
+
27
+ # Carrega a chave do .env
28
+ load_dotenv()
29
+ api_key = os.getenv("OPENROUTER_API_KEY")
30
+ # Verifica se a chave está disponível
31
+ if not api_key:
32
+ raise ValueError("❌ Variável OPENROUTER_API_KEY não encontrada.")
33
+
34
+ # Define as variáveis que o LangChain espera
35
+ os.environ["OPENAI_API_KEY"] = api_key
36
+ os.environ["OPENAI_API_BASE"] = "https://openrouter.ai/api/v1"
37
+
38
+ # Instancia o modelo
39
+ llm = ChatOpenAI(
40
+ model="deepseek/deepseek-r1:free",
41
+ temperature=0.5
42
+ )
43
+
44
+ # Função simples sem histórico
45
+ def responder(mensagem):
46
+ try:
47
+ resposta = llm.invoke(mensagem)
48
+ return resposta.content
49
+ except Exception as e:
50
+ import traceback
51
+ return f"❌ Erro:\n{traceback.format_exc()}"
52
+
53
+ # Interface Gradio simples
54
+ app = gr.Interface(
55
+ fn=responder,
56
+ inputs=gr.Textbox(placeholder="Digite sua pergunta aqui", label="Mensagem"),
57
+ outputs=gr.Textbox(label="Resposta do Chatbot"),
58
+ title="Meu Primeiro Chatbot com IA Generativa",
59
+ description="Teste do modelo DeepSeek via OpenRouter com retorno direto.",
60
+ )
61
+
62
+ app.launch(share=True)