AI Blogen - WIP - 0.0.00000.001

This commit is contained in:
AjaySi
2024-01-13 16:27:28 +05:30
parent b51e9a8c2f
commit fd7053fb4b
17 changed files with 1003 additions and 101 deletions

View File

@@ -0,0 +1,79 @@
"""
"""
import os
import logging
from pathlib import Path
import google.generativeai as genai
logging.basicConfig(level=logging.INFO, format='%(asctime)s-%(levelname)s-%(module)s-%(lineno)d-%(message)s')
from dotenv import load_dotenv
load_dotenv(Path('../../.env'))
from tenacity import (
retry,
stop_after_attempt,
wait_random_exponential,
) # for exponential backoff
@retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6))
def gemini_arxiv_img_info(img_path):
""" Get image details from arxiv papers. """
try:
genai.configure(api_key=os.getenv("API_KEY"))
except Exception as e:
logging.error(f"Could not load gemini API key: {e}")
raise e
# Set up the model
generation_config = {
"temperature": 0.9,
"top_p": 1,
"top_k": 1,
"max_output_tokens": 1096,
}
safety_settings = [{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},]
try:
model = genai.GenerativeModel(model_name="gemini-pro-vision",
generation_config=generation_config,
safety_settings=safety_settings)
except Exception as e:
logging.error(f"Could not create GenerativeModel: {e}")
raise e
# Validate that an image is present
if not (img := Path(img_path)).exists():
raise FileNotFoundError(f"Could not find image: {img}")
image_parts = [{
"mime_type": "image/png",
"data": Path(img_path).read_bytes()
},]
prompt_parts = [
"As scholar on evaluating research papers, I will provide you with an image from a research paper. Your task is to explain the image in details so that I can use it in a blog article. Explain the key findings and conclusions from the image. Your description should be in simple terms to explain to a wider audience. Explain key findings from the given image.",
image_parts[0],]
try:
response = model.generate_content(prompt_parts)
return response.text
except Exception as e:
logging.error(f"Could not generate gemini content: {e}")
raise e

View File

@@ -7,6 +7,7 @@ import google.generativeai as genai
logging.basicConfig(level=logging.INFO, format='%(asctime)s-%(levelname)s-%(module)s-%(lineno)d-%(message)s')
from dotenv import load_dotenv
load_dotenv(Path('../../.env'))
from .mistral_chat_completion import mistral_text_response
from tenacity import (
retry,
@@ -17,7 +18,7 @@ from tenacity import (
@retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6))
def gemini_text_response(prompt):
""" Provide a programming blog and get code exmaples."""
""" Common functiont to get response from gemini pro Text. """
genai.configure(api_key=os.getenv('GEMINI_API_KEY'))
# Set up the model
@@ -25,14 +26,17 @@ def gemini_text_response(prompt):
"temperature": 1,
"top_p": 1,
"top_k": 1,
"max_output_tokens": 4096,
"max_output_tokens": 6096,
}
model = genai.GenerativeModel(model_name="gemini-pro", generation_config=generation_config)
try:
response = model.generate_content(prompt)
except Exception as err:
logger.error(f"Failed to get response from Gemini: {err}. Retrying..")
gemini_research_report(query)
except Exception as err:
logger.error(f"Failed to get response from Gemini: {err}. Retrying.")
# Try with minstral.
print(f"\n\n\n--MINSTRAL--\n\n\n\n")
response = mistral_text_response(prompt)
return response
return response.text

View File

@@ -0,0 +1,40 @@
import os
import logging
from pathlib import Path
from mistralai.client import MistralClient
from mistralai.models.chat_completion import ChatMessage
logging.basicConfig(level=logging.INFO, format='%(asctime)s-%(levelname)s-%(module)s-%(lineno)d-%(message)s')
from dotenv import load_dotenv
load_dotenv(Path('../../.env'))
from tenacity import (
retry,
stop_after_attempt,
wait_random_exponential,
) # for exponential backoff
@retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6))
def mistral_text_response(prompt):
""" Common function to get text response from minstral. """
api_key = os.environ["MISTRAL_API_KEY"]
model = "mistral-medium"
client = MistralClient(api_key=api_key)
messages = [
ChatMessage(role="user", content=prompt)
]
# No streaming
chat_response = client.chat(
model=model,
messages=messages,
)
print(chat_response)
# With streaming
for chunk in client.chat_stream(model=model, messages=messages):
print(chunk)