Spaces:
Sleeping
Sleeping
| import os | |
| from langchain_community.embeddings import HuggingFaceEmbeddings | |
| from langchain_community.vectorstores import Chroma | |
| from langchain_google_genai import ChatGoogleGenerativeAI | |
| from langchain.prompts import PromptTemplate | |
| from langchain.chains import LLMChain | |
| class GeminiLLM(): | |
| def __init__(self): | |
| self.ACCESS_TOKEN = os.getenv('GOOGLE_GEMINI_TOKEN') | |
| self.model_name = "gemini-pro" | |
| def getEmbeddingsModel(self): | |
| self.embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2") | |
| return self.embeddings | |
| def getRetriver(self, documents ): | |
| vectorstore = Chroma.from_documents( | |
| documents = documents, | |
| embedding = self.embeddings, | |
| persist_directory = "chroma_db_dir", # Local mode with in-memory storage only | |
| collection_name="sermon_lab_ai" | |
| ) | |
| retriever = vectorstore.as_retriever( | |
| search_kwargs={"k": 3} | |
| ) | |
| return (retriever, vectorstore) | |
| def getLLM(self ): | |
| if os.getenv('GOOGLE_GEMINI_TOKEN') is None: | |
| raise ValueError("GOOGLE_GEMINI_TOKEN environment variable not set") | |
| else: | |
| os.environ["GOOGLE_API_KEY"] = os.getenv('GOOGLE_GEMINI_TOKEN') | |
| self.llm = ChatGoogleGenerativeAI( | |
| model = self.model_name, | |
| temperature = 0.7, | |
| top_k = 40, | |
| top_p = 1 | |
| ) | |
| return self.llm | |
| class SermonGeminiPromptTemplate(): | |
| # Example of {BIBLE_VERSICLE} | |
| # BIBLE_VERSICLE = Juan 1:1-18 | |
| custom_prompt_template_gemini = """ | |
| Usted es pastor evangélico que está preparando un sermón para su comunidad sobre {SERMON_TOPIC} | |
| Necesito que me ayudes a encontrar los versículos más relevantes de la Biblia que se relacionen con este tema. | |
| Por favor, proporcióname una lista de {CANT_VERSICULOS} versículos clave, citando el libro, capítulo y versículo. | |
| También incluye una breve frase que resuma el significado de cada versículo en relación con el tema. | |
| Asegúrate de que los versículos provengan de diferentes libros de la Biblia para tener una perspectiva amplia. | |
| Formatea la salida en una lista con viñetas. Gracias por tu ayuda. | |
| Context: {context} | |
| Solo devuelve la respuesta útil a continuación y nada más y responde siempre en español | |
| Respuesta útil: | |
| """ | |
| custom_prompt_template_gemini_buildSermonStart = """ | |
| Usted es pastor evangélico que está preparando un sermón para su comunidad. | |
| Necesito que me ayudes a elaborar un sermón sobre los versículos de la biblia | |
| en {BIBLE_VERSICLE} con la estructura: | |
| * Introducción: | |
| * Cuerpo del Sermón: | |
| * Conclusión: | |
| Context: {context} | |
| """ | |
| custom_prompt_template_gemini_buildSermonFronContext = """ | |
| Usted es pastor evangélico que está preparando un sermón para su comunidad. | |
| {SERMON_IDEA} | |
| Context: {context} | |
| Ahora ayúdame a desarrollar el sermón siguiente estas mismas ideas | |
| """ | |
| custom_prompt_template_gemini_buildSermonPrepare = """ | |
| Usted es pastor evangélico que está preparando un sermón para su comunidad. | |
| {SERMON_CONTEXT} | |
| Context: {context} | |
| Usando el texto anterior responde a la pregunta: {question} | |
| """ | |
| custom_prompt_template_gemini_buildSermonQuestion = """ | |
| Usted es pastor evangélico que está preparando un sermón para su comunidad. | |
| {SERMON_IDEA} | |
| Context: {context} | |
| Elabora una guía de preguntas que facilite la discusión bíblica en un grupo | |
| pequeño de estudio bíblico de adultos a partir del sermón en el texto anterior | |
| """ | |
| custom_prompt_template_gemini_buildSermonReflections = """ | |
| Usted es pastor evangélico que está preparando un sermón para su comunidad. | |
| {SERMON_IDEA} | |
| Context: {context} | |
| Elaborar una serie de 5 reflexiones a partir del sermón en el texto anterior | |
| """ | |
| custom_prompt_template_gemini_argumentQuestions = """ | |
| Usted es pastor evangélico que está preparando un sermón para su comunidad. | |
| A partir de este texto: | |
| \"{QUESTION_ANSWER}\" | |
| Context: {context} | |
| Argumentar con más información manteniendo el texto anterior. Use como fuente la Biblia u otros recursos religiosos. | |
| Solo devuelve la respuesta útil a continuación y nada más y responde siempre en español. Gracias por su ayuda: | |
| """ | |
| sermonPromptMenuGemini = { | |
| 'BUILD_INIT': custom_prompt_template_gemini, | |
| 'BUILD_EMPTY': custom_prompt_template_gemini_buildSermonStart, | |
| 'BUILD_FROM_IDEA': custom_prompt_template_gemini_buildSermonFronContext, | |
| 'BUILD_QUESTION': custom_prompt_template_gemini_buildSermonQuestion, | |
| 'BUILD_REFLECTIONS': custom_prompt_template_gemini_buildSermonReflections, | |
| 'BUILD_PREPARE_QUESTIONS': custom_prompt_template_gemini_buildSermonPrepare, | |
| 'BUILD_ADD_INFORMATION_TO_QUEST_ANSWER': custom_prompt_template_gemini_argumentQuestions | |
| } | |
| def __init__(self ): | |
| self.model_name = 'gemini-pro' | |
| def getSermonPromptTemplates(self): | |
| return self.sermonPromptMenuGemini | |
| def getSermonPromptTemplate(self, sermon_id): | |
| if not sermon_id in self.sermonPromptMenuGemini.values(): | |
| return None | |
| return self.sermonPromptMenuGemini[sermon_id] | |