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

partners/openai: fix depracation errors of pydantic's .dict() function (reopen #16629) #17404

Merged
merged 7 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion libs/partners/openai/langchain_openai/chat_models/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,11 @@ def lc_attributes(self) -> Dict[str, Any]:

def _create_chat_result(self, response: Union[dict, BaseModel]) -> ChatResult:
if not isinstance(response, dict):
response = response.dict()
response = (
getattr(response, "model_dump")()
if hasattr(response, "model_dump")
else response.dict()
)
for res in response["choices"]:
if res.get("finish_reason", None) == "content_filter":
raise ValueError(
Expand Down
10 changes: 7 additions & 3 deletions libs/partners/openai/langchain_openai/chat_models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ def _stream(
default_chunk_class = AIMessageChunk
for chunk in self.client.create(messages=message_dicts, **params):
if not isinstance(chunk, dict):
chunk = chunk.dict()
chunk = chunk.model_dump()
if len(chunk["choices"]) == 0:
continue
choice = chunk["choices"][0]
Expand Down Expand Up @@ -465,7 +465,11 @@ def _create_message_dicts(
def _create_chat_result(self, response: Union[dict, BaseModel]) -> ChatResult:
generations = []
if not isinstance(response, dict):
response = response.dict()
response = (
getattr(response, "model_dump")()
if hasattr(response, "model_dump")
else response.dict()
)
SavvasMohito marked this conversation as resolved.
Show resolved Hide resolved
for res in response["choices"]:
message = _convert_dict_to_message(res["message"])
generation_info = dict(finish_reason=res.get("finish_reason"))
Expand Down Expand Up @@ -499,7 +503,7 @@ async def _astream(
messages=message_dicts, **params
):
if not isinstance(chunk, dict):
chunk = chunk.dict()
chunk = chunk.model_dump()
if len(chunk["choices"]) == 0:
continue
choice = chunk["choices"][0]
Expand Down
8 changes: 4 additions & 4 deletions libs/partners/openai/langchain_openai/embeddings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def _get_len_safe_embeddings(
input=tokens[i : i + _chunk_size], **self._invocation_params
)
if not isinstance(response, dict):
response = response.dict()
response = response.model_dump()
batched_embeddings.extend(r["embedding"] for r in response["data"])

results: List[List[List[float]]] = [[] for _ in range(len(texts))]
Expand All @@ -343,7 +343,7 @@ def _get_len_safe_embeddings(
input="", **self._invocation_params
)
if not isinstance(average_embedded, dict):
average_embedded = average_embedded.dict()
average_embedded = average_embedded.model_dump()
average = average_embedded["data"][0]["embedding"]
else:
average = np.average(_result, axis=0, weights=num_tokens_in_batch[i])
Expand Down Expand Up @@ -436,7 +436,7 @@ async def _aget_len_safe_embeddings(
)

if not isinstance(response, dict):
response = response.dict()
response = response.model_dump()
batched_embeddings.extend(r["embedding"] for r in response["data"])

results: List[List[List[float]]] = [[] for _ in range(len(texts))]
Expand All @@ -453,7 +453,7 @@ async def _aget_len_safe_embeddings(
input="", **self._invocation_params
)
if not isinstance(average_embedded, dict):
average_embedded = average_embedded.dict()
average_embedded = average_embedded.model_dump()
average = average_embedded["data"][0]["embedding"]
else:
average = np.average(_result, axis=0, weights=num_tokens_in_batch[i])
Expand Down
8 changes: 4 additions & 4 deletions libs/partners/openai/langchain_openai/llms/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def _stream(
self.get_sub_prompts(params, [prompt], stop) # this mutates params
for stream_resp in self.client.create(prompt=prompt, **params):
if not isinstance(stream_resp, dict):
stream_resp = stream_resp.dict()
stream_resp = stream_resp.model_dump()
chunk = _stream_response_to_generation_chunk(stream_resp)
yield chunk
if run_manager:
Expand All @@ -275,7 +275,7 @@ async def _astream(
prompt=prompt, **params
):
if not isinstance(stream_resp, dict):
stream_resp = stream_resp.dict()
stream_resp = stream_resp.model_dump()
chunk = _stream_response_to_generation_chunk(stream_resp)
yield chunk
if run_manager:
Expand Down Expand Up @@ -347,7 +347,7 @@ def _generate(
if not isinstance(response, dict):
# V1 client returns the response in an PyDantic object instead of
# dict. For the transition period, we deep convert it to dict.
response = response.dict()
response = response.model_dump()

choices.extend(response["choices"])
_update_token_usage(_keys, response, token_usage)
Expand Down Expand Up @@ -406,7 +406,7 @@ async def _agenerate(
else:
response = await self.async_client.create(prompt=_prompts, **params)
if not isinstance(response, dict):
response = response.dict()
response = response.model_dump()
choices.extend(response["choices"])
_update_token_usage(_keys, response, token_usage)
return self.create_llm_result(
Expand Down