Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
fix: factor out repeat POST request logic (#1851)
Matthew Zhou020bbd658ebd
diff --git a/letta/llm_api/anthropic.py b/letta/llm_api/anthropic.pyindex 87f13d8ae..e2385d5de 100644--- a/letta/llm_api/anthropic.py+++ b/letta/llm_api/anthropic.py@@ -2,8 +2,7 @@ import jsonimport refrom typing import List, Optional, Union-import requests-+from letta.llm_api.helpers import make_post_requestfrom letta.schemas.message import Messagefrom letta.schemas.openai.chat_completion_request import ChatCompletionRequest, Toolfrom letta.schemas.openai.chat_completion_response import (@@ -295,7 +294,6 @@ def anthropic_chat_completions_request(inner_thoughts_xml_tag: Optional[str] = "thinking",) -> ChatCompletionResponse:"""https://docs.anthropic.com/claude/docs/tool-use"""- from letta.utils import printdurl = smart_urljoin(url, "messages")headers = {@@ -360,24 +358,5 @@ def anthropic_chat_completions_request(data.pop("user", None)data.pop("tool_choice", None)- printd(f"Sending request to {url}")- try:- response = requests.post(url, headers=headers, json=data)- printd(f"response = {response}")- response.raise_for_status() # Raises HTTPError for 4XX/5XX status- response = response.json() # convert to dict from string- printd(f"response.json = {response}")- response = convert_anthropic_response_to_chatcompletion(response_json=response, inner_thoughts_xml_tag=inner_thoughts_xml_tag)- return response- except requests.exceptions.HTTPError as http_err:- # Handle HTTP errors (e.g., response 4XX, 5XX)- printd(f"Got HTTPError, exception={http_err}, payload={data}")- raise http_err- except requests.exceptions.RequestException as req_err:- # Handle other requests-related errors (e.g., connection error)- printd(f"Got RequestException, exception={req_err}")- raise req_err- except Exception as e:- # Handle other potential errors- printd(f"Got unknown Exception, exception={e}")- raise e+ response_json = make_post_request(url, headers, data)+ return convert_anthropic_response_to_chatcompletion(response_json=response_json, inner_thoughts_xml_tag=inner_thoughts_xml_tag)diff --git a/letta/llm_api/azure_openai.py b/letta/llm_api/azure_openai.pyindex 9ab840a67..41ca4fbe2 100644--- a/letta/llm_api/azure_openai.py+++ b/letta/llm_api/azure_openai.py@@ -1,5 +1,6 @@import requests+from letta.llm_api.helpers import make_post_requestfrom letta.schemas.llm_config import LLMConfigfrom letta.schemas.openai.chat_completion_response import ChatCompletionResponsefrom letta.schemas.openai.chat_completions import ChatCompletionRequest@@ -63,7 +64,6 @@ def azure_openai_chat_completions_request(model_settings: ModelSettings, llm_config: LLMConfig, api_key: str, chat_completion_request: ChatCompletionRequest) -> ChatCompletionResponse:"""https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#chat-completions"""- from letta.utils import printdassert api_key is not None, "Missing required field when calling Azure OpenAI"@@ -79,59 +79,22 @@ def azure_openai_chat_completions_request(data.pop("tools")data.pop("tool_choice", None) # extra safe, should exist always (default="auto")- model_endpoint = get_azure_chat_completions_endpoint(model_settings.azure_base_url, llm_config.model, model_settings.api_version)- printd(f"Sending request to {model_endpoint}")- try:- response = requests.post(model_endpoint, headers=headers, json=data)- response.raise_for_status() # Raises HTTPError for 4XX/5XX status- response = response.json() # convert to dict from string- printd(f"response.json = {response}")- # NOTE: azure openai does not include "content" in the response when it is None, so we need to add it- if "content" not in response["choices"][0].get("message"):- response["choices"][0]["message"]["content"] = None- response = ChatCompletionResponse(**response) # convert to 'dot-dict' style which is the openai python client default- return response- except requests.exceptions.HTTPError as http_err:- # Handle HTTP errors (e.g., response 4XX, 5XX)- printd(f"Got HTTPError, exception={http_err}, payload={data}")- raise http_err- except requests.exceptions.RequestException as req_err:- # Handle other requests-related errors (e.g., connection error)- printd(f"Got RequestException, exception={req_err}")- raise req_err- except Exception as e:- # Handle other potential errors- printd(f"Got unknown Exception, exception={e}")- raise e+ url = get_azure_chat_completions_endpoint(model_settings.azure_base_url, llm_config.model, model_settings.api_version)+ response_json = make_post_request(url, headers, data)+ # NOTE: azure openai does not include "content" in the response when it is None, so we need to add it+ if "content" not in response_json["choices"][0].get("message"):+ response_json["choices"][0]["message"]["content"] = None+ response = ChatCompletionResponse(**response_json) # convert to 'dot-dict' style which is the openai python client default+ return responsedef azure_openai_embeddings_request(resource_name: str, deployment_id: str, api_version: str, api_key: str, data: dict) -> EmbeddingResponse:"""https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#embeddings"""- from letta.utils import printdurl = f"https://{resource_name}.openai.azure.com/openai/deployments/{deployment_id}/embeddings?api-version={api_version}"headers = {"Content-Type": "application/json", "api-key": f"{api_key}"}- printd(f"Sending request to {url}")- try:- response = requests.post(url, headers=headers, json=data)- printd(f"response = {response}")- response.raise_for_status() # Raises HTTPError for 4XX/5XX status- response = response.json() # convert to dict from string- printd(f"response.json = {response}")- response = EmbeddingResponse(**response) # convert to 'dot-dict' style which is the openai python client default- return response- except requests.exceptions.HTTPError as http_err:- # Handle HTTP errors (e.g., response 4XX, 5XX)- printd(f"Got HTTPError, exception={http_err}, payload={data}")- raise http_err- except requests.exceptions.RequestException as req_err:- # Handle other requests-related errors (e.g., connection error)- printd(f"Got RequestException, exception={req_err}")- raise req_err- except Exception as e:- # Handle other potential errors- printd(f"Got unknown Exception, exception={e}")- raise e+ response_json = make_post_request(url, headers, data)+ return EmbeddingResponse(**response_json)