Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add non-GCP PALM providers #579

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/advanced/app_types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,14 @@ app = CustomApp(
- GPT4ALL
- AZURE_OPENAI
- LLAMA2
- PALM
- Following embedding functions are available for an embedding function
- OPENAI
- HUGGING_FACE
- VERTEX_AI
- GPT4ALL
- AZURE_OPENAI
- PALM


### PersonApp
Expand Down
31 changes: 31 additions & 0 deletions embedchain/embedder/palm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import os
from typing import Optional

from langchain.embeddings import GooglePalmEmbeddings
from importlib.util import find_spec

from embedchain.config import BaseEmbedderConfig
from embedchain.embedder.base import BaseEmbedder
from embedchain.models import EmbeddingFunctions


class PalmEmbedder(BaseEmbedder):
def __init__(self, config: Optional[BaseEmbedderConfig] = None):
if find_spec("google.generativeai") is None:
raise ModuleNotFoundError(
"The google-generativeai python package is not installed. Please install it with `pip install --upgrade embedchain[palm]`" # noqa E501
)
super().__init__(config=config)
Mark-Zeng marked this conversation as resolved.
Show resolved Hide resolved

if os.getenv("GOOGLE_API_KEY") is None:
raise ValueError("GOOGLE_API_KEY environment variables not provided")

model = "models/embedding-gecko-001"
Mark-Zeng marked this conversation as resolved.
Show resolved Hide resolved
if (config is not None) and (config.model is not None):
model = config.model

embeddings = GooglePalmEmbeddings(model_name=model)
Mark-Zeng marked this conversation as resolved.
Show resolved Hide resolved
embedding_fn = BaseEmbedder._langchain_default_concept(embeddings)

self.set_embedding_fn(embedding_fn=embedding_fn)
self.set_vector_dimension(vector_dimension=EmbeddingFunctions.PALM.value)
36 changes: 36 additions & 0 deletions embedchain/llm/palm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os
from importlib.util import find_spec
from typing import Optional

from embedchain.config import BaseLlmConfig
from embedchain.helper.json_serializable import register_deserializable
from embedchain.llm.base import BaseLlm


@register_deserializable
class PalmLlm(BaseLlm):
def __init__(self, config: Optional[BaseLlmConfig] = None):
if find_spec("google.generativeai") is None:
raise ModuleNotFoundError(
"The google-generativeai python package is not installed. Please install it with `pip install --upgrade embedchain[palm]`" # noqa E501
)
super().__init__(config=config)

def get_llm_model_answer(self, prompt):
return PalmLlm._get_athrophic_answer(prompt=prompt, config=self.config)

@staticmethod
def _get_athrophic_answer(prompt: str, config: BaseLlmConfig) -> str:
if os.getenv("GOOGLE_API_KEY") is None:
raise ValueError("GOOGLE_API_KEY environment variables not provided")

from langchain.chat_models import ChatGooglePalm

model = "models/chat-bison-001"
Mark-Zeng marked this conversation as resolved.
Show resolved Hide resolved
if (config is not None) and (config.model is not None):
model = config.model

chat = ChatGooglePalm(temperature=config.temperature, top_p=config.top_p, model_name=model)
messages = BaseLlm._get_messages(prompt, system_prompt=config.system_prompt)

return chat(messages).content
1 change: 1 addition & 0 deletions embedchain/models/EmbeddingFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ class EmbeddingFunctions(Enum):
HUGGING_FACE = "HUGGING_FACE"
VERTEX_AI = "VERTEX_AI"
GPT4ALL = "GPT4ALL"
PALM = "PALM"
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ twilio = { version = "^8.5.0", optional = true }
fastapi-poe = { version = "0.0.16", optional = true }
discord = { version = "^2.3.2", optional = true }
slack-sdk = { version = "3.21.3", optional = true }

google-generativeai = { version = "^0.1.0", optional = true}


[tool.poetry.group.dev.dependencies]
Expand All @@ -120,6 +120,7 @@ isort = "^5.12.0"
streamlit = ["streamlit"]
community = ["llama-hub"]
opensource = ["sentence-transformers", "torch", "gpt4all"]
palm = ["google-generativeai"]
elasticsearch = ["elasticsearch"]
poe = ["fastapi-poe"]
discord = ["discord"]
Expand Down