Skip to content

Commit d771a75

Browse files
committed
Remove langchain!!
1 parent 3d6499f commit d771a75

File tree

12 files changed

+68
-1130
lines changed

12 files changed

+68
-1130
lines changed

app/desktop/studio_server/provider_api.py

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from datetime import datetime, timedelta
55
from typing import Any, Dict, List
66

7+
import litellm
78
import openai
89
import requests
910
from fastapi import FastAPI, HTTPException
@@ -24,7 +25,6 @@
2425
from kiln_ai.datamodel.registry import all_projects
2526
from kiln_ai.utils.config import Config
2627
from kiln_ai.utils.exhaustive_error import raise_exhaustive_enum_error
27-
from langchain_aws import ChatBedrockConverse
2828
from pydantic import BaseModel, Field
2929

3030
logger = logging.getLogger(__name__)
@@ -611,42 +611,34 @@ async def connect_bedrock(key_data: dict):
611611
detail="Access Key or Secret Key not found",
612612
)
613613
try:
614-
# Langchain API is not good... need to use env vars. Pop these in finally block
615-
os.environ["AWS_ACCESS_KEY_ID"] = access_key
616-
os.environ["AWS_SECRET_ACCESS_KEY"] = secret_key
617-
618-
# Fake model, but will get a credential error before AWS realizes it's wrong
619-
# Ensures we don't spend money on a test call
620-
llm = ChatBedrockConverse(
621-
model="fake_model",
622-
region_name="us-west-2",
614+
# Test credentials request, but invalid model so we don't use tokens
615+
await litellm.acompletion(
616+
model="bedrock/ai21.jamba-1-5-mini-v9999.8888",
617+
aws_region_name="us-west-2",
618+
aws_access_key_id=access_key,
619+
aws_secret_access_key=secret_key,
620+
messages=[{"role": "user", "content": "Hello, how are you?"}],
623621
)
624-
llm.invoke("Hello, how are you?")
625622
except Exception as e:
626-
# Check for specific error messages indicating invalid credentials
627-
if "UnrecognizedClientException" in str(
628-
e
629-
) or "InvalidSignatureException" in str(e):
623+
# Improve error message if it's a confirmed authentication error
624+
if isinstance(e, litellm.exceptions.AuthenticationError):
630625
return JSONResponse(
631626
status_code=401,
632627
content={
633628
"message": "Failed to connect to Bedrock. Invalid credentials."
634629
},
635630
)
636-
else:
637-
# We still expect an error (fake model), but not for invalid credentials which means success!
631+
# If it's a bad request, it's a valid key (but the model is fake)
632+
if isinstance(e, litellm.exceptions.BadRequestError):
638633
Config.shared().bedrock_access_key = access_key
639634
Config.shared().bedrock_secret_key = secret_key
640635
return JSONResponse(
641636
status_code=200,
642637
content={"message": "Connected to Bedrock"},
643638
)
639+
# Unknown error, raise it
640+
raise e
644641

645-
finally:
646-
os.environ.pop("AWS_ACCESS_KEY_ID")
647-
os.environ.pop("AWS_SECRET_ACCESS_KEY")
648-
649-
# we shouldn't get here, but if we do, something went wrong
650642
return JSONResponse(
651643
status_code=400,
652644
content={"message": "Unknown Bedrock Error"},

conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
from unittest.mock import patch
22

3+
import litellm
34
import pytest
45
from dotenv import load_dotenv
56
from kiln_ai.utils.config import Config
67

78

9+
@pytest.fixture(autouse=True)
10+
def _clear_httpx_clients() -> None:
11+
litellm.in_memory_llm_clients_cache.flush_cache()
12+
13+
814
@pytest.fixture(scope="session", autouse=True)
915
def load_env():
1016
load_dotenv()

libs/core/kiln_ai/adapters/adapter_registry.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from kiln_ai import datamodel
44
from kiln_ai.adapters.ml_model_list import ModelProviderName
55
from kiln_ai.adapters.model_adapters.base_adapter import AdapterConfig, BaseAdapter
6-
from kiln_ai.adapters.model_adapters.langchain_adapters import LangchainAdapter
76
from kiln_ai.adapters.model_adapters.openai_model_adapter import (
87
OpenAICompatibleAdapter,
98
OpenAICompatibleConfig,
@@ -62,7 +61,6 @@ def adapter_for_task(
6261
prompt_id=prompt_id,
6362
base_adapter_config=base_adapter_config,
6463
)
65-
# Use LangchainAdapter for the rest
6664
case ModelProviderName.groq:
6765
return OpenAICompatibleAdapter(
6866
kiln_task=kiln_task,
@@ -138,19 +136,6 @@ def adapter_for_task(
138136
provider_name=provider,
139137
),
140138
)
141-
case ModelProviderName.azure:
142-
return OpenAICompatibleAdapter(
143-
kiln_task=kiln_task,
144-
prompt_id=prompt_id,
145-
base_adapter_config=base_adapter_config,
146-
config=OpenAICompatibleConfig(
147-
litellm_provider_name="azure_ai",
148-
api_key=Config.shared().azure_ai_api_key,
149-
# base_url=Config.shared().azure_ai_api_base,
150-
model_name=model_name,
151-
provider_name=provider,
152-
),
153-
)
154139
case ModelProviderName.azure_openai:
155140
return OpenAICompatibleAdapter(
156141
kiln_task=kiln_task,

libs/core/kiln_ai/adapters/ml_model_list.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ class ModelProviderName(str, Enum):
2828
openai_compatible = "openai_compatible"
2929
anthropic = "anthropic"
3030
gemini_api = "gemini_api"
31-
# They are going to change the name 16 times so don't include "ai studio" or "foundry" in the name :P
32-
azure = "azure"
3331
azure_openai = "azure_openai"
3432

3533

@@ -876,10 +874,6 @@ class KilnModel(BaseModel):
876874
supports_data_gen=False,
877875
provider_options={"model": "microsoft/phi-4"},
878876
),
879-
KilnModelProvider(
880-
name=ModelProviderName.azure,
881-
provider_options={"model": "Phi-4"},
882-
),
883877
],
884878
),
885879
# Gemma 2 2.6b

libs/core/kiln_ai/adapters/model_adapters/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77

88
from . import (
99
base_adapter,
10-
langchain_adapters,
1110
openai_model_adapter,
1211
)
1312

1413
__all__ = [
1514
"base_adapter",
16-
"langchain_adapters",
1715
"openai_model_adapter",
1816
]

0 commit comments

Comments
 (0)