Skip to content

Commit

Permalink
[Bug fix] Fix vertex ai integration issue (#1257)
Browse files Browse the repository at this point in the history
  • Loading branch information
deshraj authored Feb 14, 2024
1 parent 036bf3a commit 0766a44
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 155 deletions.
12 changes: 3 additions & 9 deletions embedchain/embedchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,19 +429,18 @@ def _load_and_embed(

if dry_run:
return list(documents), metadatas, ids, 0

# Count before, to calculate a delta in the end.
chunks_before_addition = self.db.count()


# Filter out empty documents and ensure they meet the API requirements
valid_documents = [doc for doc in documents if doc and isinstance(doc, str)]

documents = valid_documents

# Chunk documents into batches of 2048 and handle each batch
# helps wigth large loads of embeddings that hit OpenAI limits
document_batches = [documents[i:i+2048] for i in range(0, len(documents), 2048)]
document_batches = [documents[i : i + 2048] for i in range(0, len(documents), 2048)]
for batch in document_batches:
try:
# Add only valid batches
Expand All @@ -452,12 +451,10 @@ def _load_and_embed(
# Handle the error, e.g., by logging, retrying, or skipping
pass


count_new_chunks = self.db.count() - chunks_before_addition
print(f"Successfully saved {src} ({chunker.data_type}). New chunks count: {count_new_chunks}")

return list(documents), metadatas, ids, count_new_chunks

return list(documents), metadatas, ids, count_new_chunks

@staticmethod
def _format_result(results):
Expand Down Expand Up @@ -493,9 +490,7 @@ def _retrieve_from_database(
:return: List of contents of the document that matched your query
:rtype: list[str]
"""
print("Query passed in config:", config)
query_config = config or self.llm.config
print("Final config:", query_config)
if where is not None:
where = where
else:
Expand All @@ -506,7 +501,6 @@ def _retrieve_from_database(
if self.config.id is not None:
where.update({"app_id": self.config.id})

print('Number documents', query_config)
contexts = self.db.query(
input_query=input_query,
n_results=query_config.number_documents,
Expand Down
4 changes: 3 additions & 1 deletion embedchain/llm/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from langchain.schema import BaseMessage as LCBaseMessage

from embedchain.config import BaseLlmConfig
from embedchain.config.llm.base import DEFAULT_PROMPT, DEFAULT_PROMPT_WITH_HISTORY_TEMPLATE, DOCS_SITE_PROMPT_TEMPLATE
from embedchain.config.llm.base import (DEFAULT_PROMPT,
DEFAULT_PROMPT_WITH_HISTORY_TEMPLATE,
DOCS_SITE_PROMPT_TEMPLATE)
from embedchain.helpers.json_serializable import JSONSerializable
from embedchain.memory.base import ChatHistory
from embedchain.memory.message import ChatMessage
Expand Down
3 changes: 1 addition & 2 deletions embedchain/llm/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
from typing import Any, Callable, Dict, Optional, Type, Union

from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain.schema import BaseMessage, HumanMessage, SystemMessage
from langchain_core.tools import BaseTool
from langchain_openai import ChatOpenAI
Expand Down Expand Up @@ -41,8 +42,6 @@ def _get_answer(self, prompt: str, config: BaseLlmConfig) -> str:
if config.top_p:
kwargs["model_kwargs"]["top_p"] = config.top_p
if config.stream:
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler

callbacks = config.callbacks if config.callbacks else [StreamingStdOutCallbackHandler()]
chat = ChatOpenAI(**kwargs, streaming=config.stream, callbacks=callbacks, api_key=api_key)
else:
Expand Down
17 changes: 12 additions & 5 deletions embedchain/llm/vertex_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import logging
from typing import Optional

from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain_google_vertexai import ChatVertexAI

from embedchain.config import BaseLlmConfig
from embedchain.helpers.json_serializable import register_deserializable
from embedchain.llm.base import BaseLlm
Expand All @@ -24,13 +27,17 @@ def get_llm_model_answer(self, prompt):

@staticmethod
def _get_answer(prompt: str, config: BaseLlmConfig) -> str:
from langchain_community.chat_models import ChatVertexAI

chat = ChatVertexAI(temperature=config.temperature, model=config.model)

if config.top_p and config.top_p != 1:
logging.warning("Config option `top_p` is not supported by this model.")

messages = BaseLlm._get_messages(prompt, system_prompt=config.system_prompt)

return chat(messages).content
if config.stream:
callbacks = config.callbacks if config.callbacks else [StreamingStdOutCallbackHandler()]
llm = ChatVertexAI(
temperature=config.temperature, model=config.model, callbacks=callbacks, streaming=config.stream
)
else:
llm = ChatVertexAI(temperature=config.temperature, model=config.model)

return llm.invoke(messages).content
Loading

0 comments on commit 0766a44

Please sign in to comment.