Skip to content

Teste IA #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: AI Code Reviewer

on:
pull_request:
types:
- opened
- synchronize
permissions: write-all
jobs:
review:
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v3

- name: AI Code Reviewer
uses: your-username/ai-code-reviewer@main
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
OPENAI_API_MODEL: "gpt-4" # Optional: defaults to "gpt-4"
exclude: "**/*.json, **/*.md" # Optional: exclude patterns separated by commas
73 changes: 73 additions & 0 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: CI - Lint / Tests / Deploy

on: [push]

jobs:

lint:
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v2

- name: Set up Python 3.9
uses: actions/setup-python@v2
with:
python-version: 3.9

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8

- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics

tests:
needs: lint
runs-on: ubuntu-20.04
strategy:
matrix:
python-version: [3.5, 3.6, 3.7, 3.8, 3.9]

steps:
- uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

# - name: Install dependencies
# run: |
# python -m pip install --upgrade pip
# pip install flake8 pytest
# if [ -f requirements.txt ]; then pip install -r requirements.txt; fi

# - name: Integrations Test
# run: |
# python test.py


deploy:
needs: tests
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v2

- name: Set up Python 3.9
uses: actions/setup-python@v2
with:
python-version: 3.9

- name: deploy into heroku
uses: akhileshns/[email protected]
with:
heroku_api_key: ${{secrets.HEROKU_API_KEY}}
heroku_app_name: ${{secrets.HEROKU_APP_NAME}}
heroku_email: ${{secrets.HEROKU_USER_EMAIL}}
206 changes: 206 additions & 0 deletions AMPLIFY_SETUP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
# Configuração AWS Amplify - Guia Completo

## Passo 1: Criar Funções Lambda

### 1.1 Função para Chat (Lambda)
1. Acesse AWS Lambda Console
2. Criar nova função: `secode-chat-api`
3. Runtime: Node.js 18.x
4. Copie o código abaixo na seção "Código das Funções Lambda"
5. Configurar variável de ambiente: `OPENAI_API_KEY`
6. Adicionar layer do OpenAI ou fazer upload com node_modules

### 1.2 Função para Contato (Lambda)
1. Criar nova função: `secode-contact-api`
2. Runtime: Node.js 18.x
3. Copie o código abaixo na seção "Código das Funções Lambda"

## Passo 2: Configurar API Gateway

### 2.1 Criar API REST
1. Acesse API Gateway Console
2. Criar nova "REST API"
3. Nome: `secode-api`

### 2.2 Configurar recursos
1. Criar recurso `/chat`
- Método POST → Integrar com `secode-chat-api`
- Habilitar CORS
2. Criar recurso `/contact`
- Método POST → Integrar com `secode-contact-api`
- Habilitar CORS

### 2.3 Deploy da API
1. Deploy para stage "prod"
2. Anote a URL base: `https://xxxxxxxxxx.execute-api.region.amazonaws.com/prod`

## Passo 3: Obter Chave da OpenAI

### 3.1 Criar Conta e Obter API Key
1. Acesse https://platform.openai.com
2. Crie uma conta (se não tiver uma)
3. Faça login e vá para "API keys" no menu
4. Clique em "Create new secret key"
5. Dê um nome para a chave (ex: "secode-chatbot")
6. Copie a chave gerada (ela começa com "sk-")
7. **IMPORTANTE:** Guarde essa chave em local seguro, ela não será exibida novamente

### 3.2 Configurar Chave na Função Lambda
1. No AWS Lambda Console, abra a função `secode-chat-api`
2. Vá na aba "Configuration" → "Environment variables"
3. Clique em "Edit"
4. Adicione nova variável:
- Key: `OPENAI_API_KEY`
- Value: sua chave da OpenAI (sk-...)
5. Salve as alterações

## Passo 4: Configurar Amplify

### 4.1 Variáveis de Ambiente no Amplify
No console do Amplify, adicione estas variáveis:
- `VITE_CHAT_API_URL`: `https://sua-api.execute-api.region.amazonaws.com/prod/chat`
- `VITE_CONTACT_API_URL`: `https://sua-api.execute-api.region.amazonaws.com/prod/contact`

### 4.2 Build Settings
O arquivo `amplify.yml` já está configurado:
- Build command: `npx vite build`
- Output: `dist/public`

## Código das Funções Lambda

### Chat Function (secode-chat-api)

**IMPORTANTE: Configure timeout para 30 segundos**
1. Lambda → Configuration → General configuration → Edit → Timeout: 30 segundos
2. Chamadas para OpenAI podem demorar 5-10 segundos, timeout padrão de 3s causa erro 502

```javascript
export const handler = async (event) => {
const headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token",
"Access-Control-Allow-Methods": "GET,POST,OPTIONS",
"Access-Control-Max-Age": "86400"
};

if (event.httpMethod === 'OPTIONS') {
return {
statusCode: 200,
headers,
body: JSON.stringify({})
};
}

try {
if (!event.body) {
throw new Error('Request body is missing');
}

const { message } = JSON.parse(event.body);

const response = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: "gpt-4o", // Use gpt-4o, gpt-4-turbo, ou gpt-4 (NÃO use 4.1)
messages: [{
role: "system",
content: "Você é o SecodIA, assistente virtual da Secode. A Secode oferece soluções de IA para pequenos empreendedores brasileiros, incluindo presença digital, automação e consultoria tecnológica. Seja prestativo e responda em português."
}, {
role: "user",
content: message
}],
max_tokens: 150, // Reduzido para evitar timeouts
temperature: 0.7
})
});

const data = await response.json();

return {
statusCode: 200,
headers,
body: JSON.stringify({
response: data.choices[0].message.content
})
};
} catch (error) {
return {
statusCode: 500,
headers,
body: JSON.stringify({
error: "Erro interno do servidor",
details: error.message
})
};
}
};
```

### Contact Function (secode-contact-api)
```javascript
exports.handler = async (event) => {
const headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Allow-Methods": "OPTIONS,POST,GET"
};

if (event.httpMethod === 'OPTIONS') {
return { statusCode: 200, headers, body: '' };
}

try {
const { name, email, phone, business, message } = JSON.parse(event.body);

console.log('Contato recebido:', { name, email, phone, business, message });

return {
statusCode: 201,
headers,
body: JSON.stringify({
success: true,
message: 'Contato enviado com sucesso!'
})
};
} catch (error) {
return {
statusCode: 500,
headers,
body: JSON.stringify({
error: "Erro interno do servidor"
})
};
}
};
```

## Passo 5: Deploy Final

### 5.1 Verificar Configurações
Confirme se você configurou:
- ✅ Chave OpenAI na função Lambda `secode-chat-api`
- ✅ URLs das APIs nas variáveis do Amplify
- ✅ CORS habilitado no API Gateway

### 5.2 Testar o Deploy
1. Faça um novo deploy no Amplify (ou aguarde o automático)
2. Acesse seu site em produção
3. Teste o chatbot - ele deve abrir automaticamente na primeira visita
4. Teste o formulário de contato

### 5.3 Solução de Problemas
Se o chatbot não funcionar:
- Verifique se a chave OpenAI está correta na função Lambda
- Verifique se as URLs estão corretas no Amplify
- Verifique os logs da função Lambda no CloudWatch

## Resultado Final
- Site funcionando no Amplify
- Chat com IA usando OpenAI
- Formulário de contato funcional
- URLs configuráveis via variáveis de ambiente
- Documentação completa para manutenção
Empty file.
73 changes: 73 additions & 0 deletions DEPLOY_GUIDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Guia de Deploy - Secode Website

## Opção 1: Deploy no Netlify (Recomendado)

### Passos:
1. **Conecte ao GitHub:**
- Faça push deste código para um repositório GitHub
- Acesse [netlify.com](https://netlify.com)
- Clique em "New site from Git"
- Conecte seu repositório GitHub

2. **Configurações de Build:**
- Build command: `npm run build`
- Publish directory: `dist`
- Node version: `18`

3. **Variáveis de Ambiente:**
- Vá em Site settings > Environment variables
- Adicione: `OPENAI_API_KEY` = sua chave da OpenAI

4. **Deploy:**
- O Netlify fará o deploy automaticamente
- As funções serverless em `netlify/functions/` serão deployadas automaticamente

## Opção 2: Deploy no AWS Amplify

### Para funcionar no Amplify, você precisa configurar API Gateway + Lambda:

1. **Criar funções Lambda:**
- Criar Lambda function `chatApi` usando código em `amplify/backend/function/chatApi/src/index.js`
- Criar Lambda function `contactApi` usando código em `amplify/backend/function/contactApi/src/index.js`
- Configurar variável de ambiente `OPENAI_API_KEY` em ambas as funções

2. **Configurar API Gateway:**
- Criar API REST no API Gateway
- Criar recursos `/api/chat/generate` (POST) → chatApi Lambda
- Criar recursos `/api/contacts` (POST) → contactApi Lambda
- Habilitar CORS para todos os recursos

3. **Deploy no Amplify:**
- Build command: `npm run build`
- Output directory: `dist`
- Configurar `OPENAI_API_KEY` nas variáveis de ambiente

### Detecção Automática:
O código detecta automaticamente se está rodando no Amplify (domínios *.amplifyapp.com) e usa os endpoints corretos.

### URLs das Funções:
- Chat: `/.netlify/functions/chat` (Netlify) ou sua URL da Lambda (Amplify)
- Contato: `/.netlify/functions/contact` (Netlify) ou sua URL da Lambda (Amplify)

## Arquivos Criados para Deploy:

### Para Netlify:
- `netlify/functions/chat.js` - Função do chat com OpenAI
- `netlify/functions/contact.js` - Função do formulário de contato
- `netlify.toml` - Configuração do Netlify

### Para Amplify:
- `amplify/backend/function/chatApi/` - Função Lambda do chat
- `amplify/backend/function/contactApi/` - Função Lambda do contato
- `amplify.yml` - Configuração do Amplify

## Funcionalidades:
- ✅ Chat com IA integrado à OpenAI
- ✅ Formulário de contato funcional
- ✅ Site responsivo e profissional
- ✅ Auto-abertura do chat na primeira visita
- ✅ Todas as informações da Secode atualizadas

## Importante:
- Lembre-se de configurar a variável `OPENAI_API_KEY` na plataforma escolhida
- O chat só funcionará após a configuração da chave da OpenAI
Loading