|
37 | 37 | from nat.llm.huggingface_llm import HuggingFaceConfig |
38 | 38 | from nat.llm.litellm_llm import LiteLlmModelConfig |
39 | 39 | from nat.llm.nim_llm import NIMModelConfig |
| 40 | +from nat.llm.oci_llm import OCIModelConfig |
40 | 41 | from nat.llm.openai_llm import OpenAIModelConfig |
41 | 42 | from nat.llm.utils.hooks import _create_metadata_injection_client |
42 | 43 | from nat.llm.utils.thinking import BaseThinkingInjector |
|
54 | 55 | ModelType = TypeVar("ModelType") |
55 | 56 |
|
56 | 57 |
|
| 58 | +def _get_langchain_oci_chat_model(): |
| 59 | + from langchain_oci import ChatOCIGenAI |
| 60 | + |
| 61 | + return ChatOCIGenAI |
| 62 | + |
| 63 | + |
57 | 64 | def _patch_llm_based_on_config(client: ModelType, llm_config: "LLMBaseConfig") -> ModelType: |
58 | 65 |
|
59 | 66 | from langchain_core.language_models import LanguageModelInput |
@@ -222,6 +229,57 @@ async def openai_langchain(llm_config: OpenAIModelConfig, _builder: Builder): |
222 | 229 | yield _patch_llm_based_on_config(client, llm_config) |
223 | 230 |
|
224 | 231 |
|
| 232 | +@register_llm_client(config_type=OCIModelConfig, wrapper_type=LLMFrameworkEnum.LANGCHAIN) |
| 233 | +async def oci_langchain(llm_config: OCIModelConfig, _builder: Builder): |
| 234 | + import oci |
| 235 | + from langchain_oci.common.auth import create_oci_client_kwargs |
| 236 | + |
| 237 | + validate_no_responses_api(llm_config, LLMFrameworkEnum.LANGCHAIN) |
| 238 | + |
| 239 | + ChatOCIGenAI = _get_langchain_oci_chat_model() |
| 240 | + |
| 241 | + model_kwargs: dict[str, Any] = {} |
| 242 | + if llm_config.temperature is not None: |
| 243 | + model_kwargs["temperature"] = llm_config.temperature |
| 244 | + if llm_config.top_p is not None: |
| 245 | + model_kwargs["top_p"] = llm_config.top_p |
| 246 | + if llm_config.max_tokens is not None: |
| 247 | + if llm_config.provider and llm_config.provider.lower() == "openai": |
| 248 | + model_kwargs["max_completion_tokens"] = llm_config.max_tokens |
| 249 | + else: |
| 250 | + model_kwargs["max_tokens"] = llm_config.max_tokens |
| 251 | + if llm_config.seed is not None: |
| 252 | + model_kwargs["seed"] = llm_config.seed |
| 253 | + |
| 254 | + client_kwargs = create_oci_client_kwargs( |
| 255 | + auth_type=llm_config.auth_type, |
| 256 | + service_endpoint=llm_config.endpoint, |
| 257 | + auth_file_location=llm_config.auth_file_location, |
| 258 | + auth_profile=llm_config.auth_profile, |
| 259 | + ) |
| 260 | + client_kwargs["retry_strategy"] = oci.retry.RetryStrategyBuilder( |
| 261 | + max_attempts=llm_config.max_retries + 1 # OCI SDK counts total attempts (initial + retries) |
| 262 | + ).get_retry_strategy() |
| 263 | + if llm_config.request_timeout is not None: |
| 264 | + client_kwargs["timeout"] = (10, llm_config.request_timeout) |
| 265 | + oci_client = oci.generative_ai_inference.GenerativeAiInferenceClient(**client_kwargs) |
| 266 | + |
| 267 | + client = ChatOCIGenAI( |
| 268 | + client=oci_client, |
| 269 | + model_id=llm_config.model_name, |
| 270 | + service_endpoint=llm_config.endpoint, |
| 271 | + compartment_id=llm_config.compartment_id, |
| 272 | + auth_type=llm_config.auth_type, |
| 273 | + auth_profile=llm_config.auth_profile, |
| 274 | + auth_file_location=llm_config.auth_file_location, |
| 275 | + provider=llm_config.provider, |
| 276 | + is_stream=getattr(llm_config, "stream", False), |
| 277 | + model_kwargs=model_kwargs or None, |
| 278 | + ) |
| 279 | + |
| 280 | + yield _patch_llm_based_on_config(client, llm_config) |
| 281 | + |
| 282 | + |
225 | 283 | @register_llm_client(config_type=DynamoModelConfig, wrapper_type=LLMFrameworkEnum.LANGCHAIN) |
226 | 284 | async def dynamo_langchain(llm_config: DynamoModelConfig, _builder: Builder): |
227 | 285 | """ |
|
0 commit comments