Skip to content

Commit

Permalink
not yet fully working
Browse files Browse the repository at this point in the history
  • Loading branch information
ajit283 committed Apr 15, 2024
1 parent 236695f commit c1cca76
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 14 deletions.
166 changes: 166 additions & 0 deletions goldenverba/components/generation/GeminiGenerator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import asyncio
import os
from collections.abc import Iterator
import vertexai.preview
from vertexai.preview.generative_models import GenerativeModel, Content, Part
from wasabi import msg

from dotenv import load_dotenv

from goldenverba.components.generation.interface import Generator

load_dotenv()


class GeminiGenerator(Generator):
"""
Gemini Generator.
"""

def __init__(self):
super().__init__()
self.name = "GeminiGenerator"
self.description = "Generator using Google's Gemini 1.5 Pro model"
self.requires_library = ["google-cloud-aiplatform"]
self.requires_env = [
"GOOGLE_APPLICATION_CREDENTIALS",
"GOOGLE_CLOUD_PROJECT",
]
self.streamable = True
self.model_name = os.getenv("GEMINI_MODEL", "gemini-1.5-pro-preview-0409")
self.context_window = 10000
self.system_instruction = "You are Verba, The Golden RAGtriever, a chatbot for Retrieval Augmented Generation (RAG). You will receive a user query and context pieces that have a semantic similarity to that specific query. Please answer these user queries only their provided context. If the provided documentation does not provide enough information, say so. If the user asks questions about you as a chatbot specifially, answer them naturally. If the answer requires code examples encapsulate them with ```programming-language-name ```. Don't do pseudo-code."

async def generate(
self,
queries: list[str],
context: list[str],
conversation: dict = None,
) -> str:
"""Generate an answer based on a list of queries and list of contexts, and includes conversational context
@parameter: queries : list[str] - List of queries
@parameter: context : list[str] - List of contexts
@parameter: conversation : dict - Conversational context
@returns str - Answer generated by the Generator.
"""
if conversation is None:
conversation = {}
messages = self.prepare_messages(queries, context, conversation)

try:

project_id = os.getenv("GOOGLE_CLOUD_PROJECT")

REGION = "us-central1"
vertexai.init(project=project_id, location=REGION)

generative_multimodal_model = GenerativeModel(
"gemini-1.5-pro-preview-0409",
system_instruction=self.system_instruction,
)
# response = generative_multimodal_model.generate_content(messages)

completion = await asyncio.to_thread(
generative_multimodal_model.generate_content, **messages
)
system_msg = str(completion.candidates[0].content.parts[0].text)

except Exception:
raise

return system_msg

async def generate_stream(
self,
queries: list[str],
context: list[str],
conversation: dict = None,
) -> Iterator[dict]:
"""Generate a stream of response dicts based on a list of queries and list of contexts, and includes conversational context
@parameter: queries : list[str] - List of queries
@parameter: context : list[str] - List of contexts
@parameter: conversation : dict - Conversational context
@returns Iterator[dict] - Token response generated by the Generator in this format {system:TOKEN, finish_reason:stop or empty}.
"""
if conversation is None:
conversation = {}
messages = self.prepare_messages(queries, context, conversation)

try:

project_id = os.getenv("GOOGLE_CLOUD_PROJECT")

REGION = "us-central1"
vertexai.init(project=project_id, location=REGION)

generative_multimodal_model = GenerativeModel(
"gemini-1.5-pro-preview-0409",
system_instruction=self.system_instruction,
)
# response = generative_multimodal_model.generate_content(messages)

completion = await generative_multimodal_model.generate_content_async(
stream=True, contents=messages
)

print("using google gemini")

iter = completion.__aiter__()

try:
while True:
chunk = await iter.__anext__()
if len(chunk.candidates) > 0:
msg.info(chunk.candidates[0])
if len(chunk.candidates[0].content.parts[0].text) > 0:
yield {
"message": chunk.candidates[0].content.parts[0].text,
"finish_reason": chunk.candidates[0].finish_reason,
}
else:
yield {
"message": "",
"finish_reason": chunk.candidates[0].finish_reason,
}
except StopAsyncIteration:
pass

except Exception:
raise

def prepare_messages(
self, queries: list[str], context: list[str], conversation: dict[str, str]
) -> list[Content]:
"""
Prepares a list of messages formatted for a Retrieval Augmented Generation chatbot system, including system instructions, previous conversation, and a new user query with context.
@parameter queries: A list of strings representing the user queries to be answered.
@parameter context: A list of strings representing the context information provided for the queries.
@parameter conversation: A list of previous conversation messages that include the role and content.
@returns A list of message dictionaries formatted for the chatbot. This includes an initial system message, the previous conversation messages, and the new user query encapsulated with the provided context.
Each message in the list is a dictionary with 'role' and 'content' keys, where 'role' is either 'system' or 'user', and 'content' contains the relevant text. This will depend on the LLM used.
"""
messages = []

for message in conversation:
messages.append(
Content(role=message.type, parts=[Part.from_text(message.content)])
)

query = " ".join(queries)
user_context = " ".join(context)

messages.append(
Content(
role="user",
parts=[
Part.from_text(
f"Please answer this query: '{query}' with this provided context: {user_context}"
)
],
)
)

return messages
2 changes: 2 additions & 0 deletions goldenverba/components/generation/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from goldenverba.components.generation.GPT4Generator import GPT4Generator
from goldenverba.components.generation.interface import Generator
from goldenverba.components.generation.Llama2Generator import Llama2Generator
from goldenverba.components.generation.GeminiGenerator import GeminiGenerator


class GeneratorManager:
Expand All @@ -17,6 +18,7 @@ def __init__(self):
"GPT3Generator": GPT3Generator(),
"CohereGenerator": CohereGenerator(),
"Llama2Generator": Llama2Generator(),
"GeminiGenerator": GeminiGenerator(),
}
self.selected_generator: Generator = self.generators["GPT3Generator"]

Expand Down
4 changes: 2 additions & 2 deletions goldenverba/server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ async def websocket_generate_stream(websocket: WebSocket):
[payload.query], [payload.context], payload.conversation
):
full_text += chunk["message"]
if chunk["finish_reason"] == "stop":
if chunk["finish_reason"] == "stop" or chunk["finish_reason"] == "STOP":
chunk["full_text"] = full_text
await websocket.send_json(chunk)

Expand All @@ -613,7 +613,7 @@ async def websocket_generate_stream(websocket: WebSocket):
await websocket.send_json(
{"message": e, "finish_reason": "stop", "full_text": e}
)
msg.good("Succesfully streamed answer")
msg.good("Successfully streamed answer")


# Retrieve auto complete suggestions based on user input
Expand Down
49 changes: 37 additions & 12 deletions goldenverba/verba_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def setup_client(self):
openai.api_version = os.getenv("OPENAI_API_VERSION")

if os.getenv("OPENAI_API_TYPE") == "azure":
openai_header_key_name = "X-Azure-Api-Key"
openai_header_key_name = "X-Azure-Api-Key"

if openai_key != "":
additional_header[openai_header_key_name] = openai_key
Expand Down Expand Up @@ -273,6 +273,12 @@ def verify_installed_libraries(self) -> None:
self.installed_libraries["openai"] = True
except Exception:
self.installed_libraries["openai"] = False
try:
import vertexai

self.installed_libraries["google-cloud-aiplatform"] = True
except Exception:
self.installed_libraries["google-cloud-aiplatform"] = False

try:
import cohere
Expand Down Expand Up @@ -356,7 +362,7 @@ def verify_variables(self) -> None:
self.environment_variables["LLAMA2-7B-CHAT-HF"] = True
else:
self.environment_variables["LLAMA2-7B-CHAT-HF"] = False

# OpenAI API Type, should be set to "azure" if using Azure OpenAI
if os.environ.get("OPENAI_API_TYPE", "") != "":
self.environment_variables["OPENAI_API_TYPE"] = True
Expand All @@ -368,33 +374,50 @@ def verify_variables(self) -> None:
self.environment_variables["OPENAI_API_VERSION"] = True
else:
self.environment_variables["OPENAI_API_VERSION"] = False
# OpenAI API Version
if os.environ.get("OPENAI_API_VERSION", "") != "":
self.environment_variables["OPENAI_API_VERSION"] = True
else:
self.environment_variables["OPENAI_API_VERSION"] = False
# OpenAI API Version
if os.environ.get("GOOGLE_CLOUD_PROJECT", "") != "":
self.environment_variables["GOOGLE_CLOUD_PROJECT"] = True
else:
self.environment_variables["GOOGLE_CLOUD_PROJECT"] = False
# OpenAI API Version
if os.environ.get("GOOGLE_APPLICATION_CREDENTIALS", "") != "":
self.environment_variables["GOOGLE_APPLICATION_CREDENTIALS"] = True
else:
self.environment_variables["GOOGLE_APPLICATION_CREDENTIALS"] = False

# Azure openai ressource name, mandatory when using Azure, should be XXX when endpoint is https://XXX.openai.azure.com
if os.environ.get("AZURE_OPENAI_RESOURCE_NAME", "") != "":
self.environment_variables["AZURE_OPENAI_RESOURCE_NAME"] = True
else:
self.environment_variables["AZURE_OPENAI_RESOURCE_NAME"] = False

#Model used for embeddings. mandatory when using Azure. Typically "text-embedding-ada-002"
# Model used for embeddings. mandatory when using Azure. Typically "text-embedding-ada-002"
if os.environ.get("AZURE_OPENAI_EMBEDDING_MODEL", "") != "":
self.environment_variables["AZURE_OPENAI_EMBEDDING_MODEL"] = True
else:
self.environment_variables["AZURE_OPENAI_EMBEDDING_MODEL"] = False

#Model used for queries. mandatory when using Azure, but can also be used to change the model used for queries when using OpenAI.
# Model used for queries. mandatory when using Azure, but can also be used to change the model used for queries when using OpenAI.
if os.environ.get("OPENAI_MODEL", "") != "":
self.environment_variables["OPENAI_MODEL"] = True
else:
self.environment_variables["OPENAI_MODEL"] = False

if os.environ.get("OPENAI_API_TYPE", "")=="azure":
if not(
self.environment_variables["OPENAI_BASE_URL"] and
self.environment_variables["AZURE_OPENAI_RESOURCE_NAME"] and
self.environment_variables["AZURE_OPENAI_EMBEDDING_MODEL"] and
self.environment_variables["OPENAI_MODEL"]
if os.environ.get("OPENAI_API_TYPE", "") == "azure":
if not (
self.environment_variables["OPENAI_BASE_URL"]
and self.environment_variables["AZURE_OPENAI_RESOURCE_NAME"]
and self.environment_variables["AZURE_OPENAI_EMBEDDING_MODEL"]
and self.environment_variables["OPENAI_MODEL"]
):
raise EnvironmentError("Missing environment variables. When using Azure OpenAI, you need to set OPENAI_BASE_URL, AZURE_OPENAI_RESOURCE_NAME, AZURE_OPENAI_EMBEDDING_MODEL and OPENAI_MODEL. Please check documentation.")
raise EnvironmentError(
"Missing environment variables. When using Azure OpenAI, you need to set OPENAI_BASE_URL, AZURE_OPENAI_RESOURCE_NAME, AZURE_OPENAI_EMBEDDING_MODEL and OPENAI_MODEL. Please check documentation."
)

def get_schemas(self) -> dict:
"""
Expand Down Expand Up @@ -599,7 +622,9 @@ async def generate_stream_answer(

else:
full_text = ""
async for result in self.generator_manager.selected_generator.generate_stream(
async for (
result
) in self.generator_manager.selected_generator.generate_stream(
queries, contexts, conversation
):
full_text += result["message"]
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"click==8.1.7",
"asyncio",
"tiktoken==0.5.1",
"google-cloud-aiplatform==1.47.0",
"cohere==4.33",
"requests",
"pypdf2",
Expand Down

0 comments on commit c1cca76

Please sign in to comment.