Skip to content

Commit

Permalink
[chore]
Browse files Browse the repository at this point in the history
  • Loading branch information
keenborder786 committed Jan 25, 2025
1 parent afd5979 commit 2d0188a
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions libs/community/langchain_community/chat_models/perplexity.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
from __future__ import annotations

import logging
from operator import itemgetter
from typing import (
Any,
Dict,
Iterator,
List,
Literal,
Mapping,
Optional,
Tuple,
Type,
TypeVar,
Union,
Literal
cast,
)

from langchain_core.callbacks import CallbackManagerForLLMRun
Expand All @@ -36,12 +39,10 @@
SystemMessageChunk,
ToolMessageChunk,
)
from langchain_core.output_parsers import JsonOutputParser
from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult
from langchain_core.runnables import Runnable
from langchain_core.utils import (
from_env,
get_pydantic_field_names,
)
from langchain_core.runnables import Runnable, RunnableMap, RunnablePassthrough, chain
from langchain_core.utils import from_env, get_pydantic_field_names
from pydantic import BaseModel, ConfigDict, Field, model_validator
from typing_extensions import Self

Expand All @@ -50,6 +51,17 @@
_DictOrPydantic = Union[Dict, _BM]

logger = logging.getLogger(__name__)
from langchain_core.utils.function_calling import convert_to_openai_function

Check failure on line 54 in libs/community/langchain_community/chat_models/perplexity.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.13

Ruff (E402)

langchain_community/chat_models/perplexity.py:54:1: E402 Module level import not at top of file

Check failure on line 54 in libs/community/langchain_community/chat_models/perplexity.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.9

Ruff (E402)

langchain_community/chat_models/perplexity.py:54:1: E402 Module level import not at top of file
from langchain_core.utils.pydantic import (
PydanticBaseModel,
TypeBaseModel,
is_basemodel_subclass,
)

Check failure on line 59 in libs/community/langchain_community/chat_models/perplexity.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.13

Ruff (E402)

langchain_community/chat_models/perplexity.py:55:1: E402 Module level import not at top of file

Check failure on line 59 in libs/community/langchain_community/chat_models/perplexity.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.9

Ruff (E402)

langchain_community/chat_models/perplexity.py:55:1: E402 Module level import not at top of file


def _is_pydantic_class(obj: Any) -> bool:
return isinstance(obj, type) and is_basemodel_subclass(obj)


def _convert_to_openai_response_format(
schema: Union[Dict[str, Any], Type], *, strict: Optional[bool] = None
Expand Down Expand Up @@ -87,6 +99,18 @@ def _convert_to_openai_response_format(
raise ValueError(msg)
return response_format


@chain
def _oai_structured_outputs_parser(ai_msg: AIMessage) -> PydanticBaseModel:
if ai_msg.additional_kwargs.get("parsed"):
return ai_msg.additional_kwargs["parsed"]
else:
raise ValueError(
"Structured Output response does not have a 'parsed' field nor a 'refusal' "
f"field. Received message:\n\n{ai_msg}"
)


class ChatPerplexity(BaseChatModel):
"""`Perplexity AI` Chat models API.
Expand Down Expand Up @@ -343,6 +367,7 @@ def with_structured_output(
)
response_format = _convert_to_openai_response_format(schema, strict=strict)
llm = self.bind(response_format=response_format)
is_pydantic_schema = _is_pydantic_class(schema)
if is_pydantic_schema:
output_parser = _oai_structured_outputs_parser.with_types(
output_type=cast(type, schema)
Expand Down

0 comments on commit 2d0188a

Please sign in to comment.