Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

Added: Google Gemini embeddings endpoints Fixed: Test suite breaking syntax errors in tests/test_base_function and tests/test_sdk_client

tarunkumark0f095a9ccf2c
diff --git a/letta/agent.py b/letta/agent.py
index bb082fbf7..a840c2649 100644
--- a/letta/agent.py
+++ b/letta/agent.py
@@ -260,6 +260,7 @@ class Agent(BaseAgent):
error_msg: str,
tool_call_id: str,
function_name: str,
+ function_args: str,
function_response: str,
messages: List[Message],
include_function_failed_message: bool = False,
@@ -543,7 +544,7 @@ class Agent(BaseAgent):
if function_response_string.startswith(ERROR_MESSAGE_PREFIX):
error_msg = function_response_string
messages = self._handle_function_error_response(
- error_msg, tool_call_id, function_name, function_response, messages, include_function_failed_message=True
+ error_msg, tool_call_id, function_name, function_args, function_response, messages, include_function_failed_message=True
)
return messages, False, True # force a heartbeat to allow agent to handle error
diff --git a/letta/embeddings.py b/letta/embeddings.py
index e588f17ab..6541cea36 100644
--- a/letta/embeddings.py
+++ b/letta/embeddings.py
@@ -167,6 +167,27 @@ class OllamaEmbeddings:
return response_json["embedding"]
+class GoogleEmbeddings:
+ def __init__(self, api_key: str, model: str, base_url: str):
+ self.api_key = api_key
+ self.model = model
+ self.base_url = base_url # Expected to be "https://generativelanguage.googleapis.com"
+
+ def get_text_embedding(self, text: str):
+ import httpx
+
+ headers = {"Content-Type": "application/json"}
+ # Build the URL based on the provided base_url, model, and API key.
+ url = f"{self.base_url}/v1beta/models/{self.model}:embedContent?key={self.api_key}"
+ payload = {"model": self.model, "content": {"parts": [{"text": text}]}}
+ with httpx.Client() as client:
+ response = client.post(url, headers=headers, json=payload)
+ # Raise an error for non-success HTTP status codes.
+ response.raise_for_status()
+ response_json = response.json()
+ return response_json["embedding"]["values"]
+
+
def query_embedding(embedding_model, query_text: str):
"""Generate padded embedding for querying database"""
query_vec = embedding_model.get_text_embedding(query_text)
@@ -237,5 +258,14 @@ def embedding_model(config: EmbeddingConfig, user_id: Optional[uuid.UUID] = None
)
return model
+ elif endpoint_type == "google_ai":
+ assert all([model_settings.gemini_api_key is not None, model_settings.gemini_base_url is not None])
+ model = GoogleEmbeddings(
+ model=config.embedding_model,
+ api_key=model_settings.gemini_api_key,
+ base_url=model_settings.gemini_base_url,
+ )
+ return model
+
else:
raise ValueError(f"Unknown endpoint type {endpoint_type}")
diff --git a/letta/settings.py b/letta/settings.py
index 80b60d048..967e66d1b 100644
--- a/letta/settings.py
+++ b/letta/settings.py
@@ -85,7 +85,7 @@ class ModelSettings(BaseSettings):
# google ai
gemini_api_key: Optional[str] = None
-
+ gemini_base_url: str = "https://generativelanguage.googleapis.com/"
# together
together_api_key: Optional[str] = None
diff --git a/tests/test_base_functions.py b/tests/test_base_functions.py
index b5ce51042..79926aafc 100644
--- a/tests/test_base_functions.py
+++ b/tests/test_base_functions.py
@@ -180,7 +180,9 @@ def test_send_message_to_agent(client, agent_obj, other_agent_obj):
found = True
break
- print(f"In context messages of the sender agent (without system):\n\n{"\n".join([m.text for m in in_context_messages[1:]])}")
+ # Compute the joined string first
+ joined_messages = "\n".join([m.text for m in in_context_messages[1:]])
+ print(f"In context messages of the sender agent (without system):\n\n{joined_messages}")
if not found:
raise Exception(f"Was not able to find an instance of the target snippet: {target_snippet}")
diff --git a/tests/test_sdk_client.py b/tests/test_sdk_client.py
index 7c2325bd2..dfadc5d0e 100644
--- a/tests/test_sdk_client.py
+++ b/tests/test_sdk_client.py
@@ -117,7 +117,7 @@ def test_shared_blocks(client: LettaSDKClient):
)
assert (
"charles" in client.agents.core_memory.retrieve_block(agent_id=agent_state2.id, block_label="human").value.lower()
- ), f"Shared block update failed {client.agents.core_memory.retrieve_block(agent_id=agent_state2.id, block_label="human").value}"
+ ), f"Shared block update failed {client.agents.core_memory.retrieve_block(agent_id=agent_state2.id, block_label='human').value}"
# cleanup
client.agents.delete(agent_state1.id)