Skip to content

Commit

Permalink
Update imports from langchain-openai.
Browse files Browse the repository at this point in the history
  • Loading branch information
qiuosier committed Sep 30, 2024
1 parent 05fb9f5 commit 01c499b
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions libs/community/langchain_community/chat_models/oci_data_science.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@
from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult
from langchain_core.runnables import Runnable, RunnableMap, RunnablePassthrough
from langchain_core.utils.function_calling import convert_to_openai_tool
from langchain_openai.chat_models.base import (
_convert_delta_to_message_chunk,
_convert_message_to_dict,
_convert_dict_to_message,
)

from pydantic import BaseModel, Field
from ads.llm.langchain.plugins.llms.oci_data_science_model_deployment_endpoint import (
Expand Down Expand Up @@ -191,6 +186,20 @@ def _construct_json_body(self, messages: list, params: dict) -> dict:
"""Stop words to use when generating. Model output is cut off
at the first occurrence of any of these substrings."""

@pre_init
def validate_environment( # pylint: disable=no-self-argument
cls, values: Dict
) -> Dict:
try:
import langchain_openai

except ImportError as ex:
raise ImportError(
"Could not import langchain_openai package. "
"Please install it with `pip install langchain_openai`."
) from ex
return values

@property
def _llm_type(self) -> str:
"""Return type of llm."""
Expand Down Expand Up @@ -545,6 +554,8 @@ def _construct_json_body(self, messages: list, params: dict) -> dict:
converted messages and additional parameters.
"""
from langchain_openai.chat_models.base import _convert_message_to_dict

return {
"messages": [_convert_message_to_dict(m) for m in messages],
**params,
Expand All @@ -571,6 +582,8 @@ def _process_stream_response(
ValueError: If the response JSON is not well-formed or does not
contain the expected structure.
"""
from langchain_openai.chat_models.base import _convert_delta_to_message_chunk

try:
choice = response_json["choices"][0]
if not isinstance(choice, dict):
Expand Down Expand Up @@ -609,6 +622,8 @@ def _process_response(self, response_json: dict) -> ChatResult:
contain the expected structure.
"""
from langchain_openai.chat_models.base import _convert_dict_to_message

generations = []
try:
choices = response_json["choices"]
Expand Down

0 comments on commit 01c499b

Please sign in to comment.