Skip to content

Commit 1bab293

Browse files
committed
Update integration with Google Gemini API; replace Cohere references, add jeannie.py for content generation, and modify README accordingly
1 parent afbe782 commit 1bab293

7 files changed

+32
-33
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ __pycache__/
77
pynews*
88
format.json
99
export.py
10+
worker.py
1011

1112
# C extensions
1213
*.so

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ Este Scraper utiliza IA para que não seja necessário a sua atualização const
3030
https://docs.docker.com/compose/install/
3131

3232

33-
### Crie uma chave junto a COHERE
33+
### Crie uma chave GEMINI API junto a Google
3434

35-
- https://docs.cohere.com/
35+
- https://aistudio.google.com/
3636
<p>
3737

38-
Atualize o valor em prompt.py
38+
Atualize o valor em jeannie.py
3939
```
4040
...
41-
os.environ["COHERE_API_KEY"] = "<YOUR-API-KEY>"
41+
GOOGLE_API_KEY = "<YOUR-API-KEY>"
4242
...
4343
```
4444

app/bibliotecas.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"Django": {
4545
"library_name": "Django",
4646
"releases_url": "https://pypi.org/project/Django/",
47-
"logo": "https://static.djangoproject.com/img/logos/django-logo-negative.png",
47+
"logo": "https://www.djangoproject.com/m/img/logos/django-logo-positive.png",
4848
"repository": "https://github.com/django/django",
4949
},
5050
"Seaborn": {

app/getNews.py

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import asyncio
22
import json
33
import sys
4-
import time
54
from datetime import datetime
65
from json import dump
76

@@ -81,6 +80,9 @@ async def fetch(self, lib, url_name):
8180
pynews[lib["library_name"]] = response
8281
elif response.get("news"):
8382
pynews[lib["library_name"]] = response
83+
pynews[lib["library_name"]]["library_name"] = lib[
84+
"library_name"
85+
]
8486
pynews[lib["library_name"]]["logo"] = bibliotecas[
8587
lib["library_name"]
8688
]["logo"]
@@ -100,15 +102,7 @@ async def main_loop(self, url_name, list_libs):
100102
await asyncio.gather(*tasks)
101103

102104
async def get(self, url_name):
103-
for i in range(0, len(self.study_case), 10):
104-
batch = {
105-
k: self.study_case[k]
106-
for k in list(self.study_case.keys())[i : i + 10]
107-
}
108-
await self.main_loop(url_name, list(batch.keys()))
109-
time.sleep(
110-
60
111-
) # O Cohere gratuíto permite somente 10 requisições por minuto
105+
await self.main_loop(url_name, list(self.study_case.keys()))
112106

113107

114108
async def main():

app/jeannie.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import google.generativeai as genai
2+
3+
GOOGLE_API_KEY = "<YOUR-API-KEY>"
4+
5+
genai.configure(api_key=GOOGLE_API_KEY)
6+
7+
model = genai.GenerativeModel("gemini-1.5-flash")
8+
9+
10+
def generate_content(prompt):
11+
12+
response = model.generate_content(prompt)
13+
14+
return response.text

app/prompt.py

+6-16
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import json
22
import os
33
from enum import Enum
4-
from typing import List, Optional
4+
from typing import List
55

6-
import cohere
76
import pydantic
7+
from jeannie import generate_content
88

9-
os.environ["COHERE_API_KEY"] = "<YOUR-"API"-KEY>"
9+
os.environ["COHERE_API_KEY"] = "niT0L5XZyimfgvLYhzviA3Xs11cHSNMjkwzzQ9OB"
1010

1111

1212
class Tags(str, Enum):
@@ -48,25 +48,14 @@ class Resume(pydantic.BaseModel):
4848

4949

5050
class Smart:
51-
def __init__(self):
52-
self.llm = cohere.ClientV2(api_key=os.environ["COHERE_API_KEY"])
53-
5451
def answer(self, path, lib, html):
5552
if path == "releases_url":
5653
return self.find_release(lib["library_name"], html)
5754
elif path == "releases_doc_url":
5855
return self.create_resume(lib, html)
5956

6057
def query(self, prompt):
61-
return self.llm.chat(
62-
model="command-r-plus-08-2024",
63-
messages=[
64-
{
65-
"role": "user",
66-
"content": prompt,
67-
}
68-
],
69-
)
58+
return generate_content(prompt)
7059

7160
def find_release(self, lib, html):
7261
prompt = f"""
@@ -120,6 +109,8 @@ def create_resume(self, lib, html):
120109
Não seja criativo na formatação, responda exatamente como o schema fornecido.
121110
Verifique se sua resposta está no formato JSON para que seja possível usar o comando json.loads do python, Caso não esteja, formate-a corretamente.
122111
112+
Sempre responda no idioma Português do Brasil
113+
123114
response_format:
124115
{json.dumps(Resume.model_json_schema())}
125116
"""
@@ -128,7 +119,6 @@ def create_resume(self, lib, html):
128119
def reply(self, prompt):
129120

130121
response = self.query(prompt)
131-
response = response.message.content[0].text
132122
response = response.replace("```json", "")
133123
response = response.replace("```", "")
134124
response = response.replace("\n", "")

requirements.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
babel==2.15.0
2-
cohere==5.13.3
3-
crawl4ai==0.4.247
2+
crawl4ai==0.4.247
3+
google-generativeai==0.8.4

0 commit comments

Comments
 (0)