Skip to content

Commit b33ff20

Browse files
authored
Merge pull request #38 from UiPath/sarath/change-prompt
Migrate custom LLM to LLM Gateway Passthrough API
2 parents 45da5bf + ce792eb commit b33ff20

4 files changed

Lines changed: 37 additions & 5 deletions

File tree

backend/danswer/configs/model_configs.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@
8484
GEN_AI_IDENTITY_ENDPOINT = os.environ.get("GEN_AI_IDENTITY_ENDPOINT") or None
8585
GEN_AI_CLIENT_ID = os.environ.get("GEN_AI_CLIENT_ID") or None
8686
GEN_AI_CLIENT_SECRET = os.environ.get("GEN_AI_CLIENT_SECRET") or None
87+
GEN_AI_ACCOUNT_ID = os.environ.get("GEN_AI_ACCOUNT_ID") or None
88+
GEN_AI_TENANT_ID = os.environ.get("GEN_AI_TENANT_ID") or None
8789
# Number of tokens from chat history to include at maximum
8890
# 3000 should be enough context regardless of use, no need to include as much as possible
8991
# as this drives up the cost unnecessarily

backend/danswer/llm/custom_llm.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
from langchain_core.messages import BaseMessage
99
from requests import Timeout
1010

11+
from danswer.configs.model_configs import GEN_AI_ACCOUNT_ID
1112
from danswer.configs.model_configs import GEN_AI_API_VERSION
1213
from danswer.configs.model_configs import GEN_AI_CLIENT_ID
1314
from danswer.configs.model_configs import GEN_AI_CLIENT_SECRET
1415
from danswer.configs.model_configs import GEN_AI_IDENTITY_ENDPOINT
1516
from danswer.configs.model_configs import GEN_AI_MAX_OUTPUT_TOKENS
17+
from danswer.configs.model_configs import GEN_AI_TENANT_ID
1618
from danswer.llm.interfaces import LLM
1719
from danswer.llm.interfaces import LLMConfig
1820
from danswer.llm.interfaces import ToolChoiceOptions
@@ -66,10 +68,12 @@ def __init__(
6668
api_key: str | None,
6769
timeout: int,
6870
endpoint: str
69-
| None = "https://alpha.uipath.com/llmgateway_/openai/deployments/gpt-4o-mini-2024-07-18/chat/completions?api-version=2024-06-01",
71+
| None = "https://alpha.uipath.com/{account_id}/{tenant_id}/llmgateway_/api/raw/vendor/openai/model/gpt-4o-2024-11-20/completions",
7072
identity_url: str | None = GEN_AI_IDENTITY_ENDPOINT,
7173
client_id: str | None = GEN_AI_CLIENT_ID,
7274
client_secret: str | None = GEN_AI_CLIENT_SECRET,
75+
account_id: str | None = GEN_AI_ACCOUNT_ID,
76+
tenant_id: str | None = GEN_AI_TENANT_ID,
7377
max_output_tokens: int = int(GEN_AI_MAX_OUTPUT_TOKENS),
7478
api_version: str | None = GEN_AI_API_VERSION,
7579
):
@@ -97,6 +101,18 @@ def __init__(
97101
"client_secret for the model server."
98102
)
99103

104+
if not account_id:
105+
raise ValueError(
106+
"Cannot point Danswer to a custom LLM server without providing the "
107+
"account_id (GEN_AI_ACCOUNT_ID) for the model server."
108+
)
109+
110+
if not tenant_id:
111+
raise ValueError(
112+
"Cannot point Danswer to a custom LLM server without providing the "
113+
"tenant_id (GEN_AI_TENANT_ID) for the model server."
114+
)
115+
100116
# TODO: implement api versions for endpoints and add those to model
101117
# if not api_version:
102118
# raise ValueError(
@@ -107,7 +123,9 @@ def __init__(
107123
self._identity_url = identity_url
108124
self._client_id = client_id
109125
self._client_secret = client_secret
110-
self._endpoint = endpoint
126+
self._account_id = account_id
127+
self._tenant_id = tenant_id
128+
self._endpoint = endpoint.format(account_id=account_id, tenant_id=tenant_id)
111129
self._max_output_tokens = max_output_tokens
112130
self._timeout = timeout
113131
self.token = self._get_token()
@@ -123,11 +141,13 @@ def __init__(
123141
def _execute(self, input: LanguageModelInput) -> AIMessage:
124142
headers = {
125143
"Content-Type": "application/json",
126-
"X-UiPath-LlmGateway-RequestedFeature": "ChatWithAssistant",
127-
"X-UiPath-LlmGateway-RequestingFeature": "ChatWithAssistant",
144+
"Authorization": "Bearer " + self.token,
128145
"X-UiPath-LlmGateway-RequestingProduct": "darwin",
146+
"X-UiPath-LlmGateway-RequestingFeature": "ChatWithAssistant",
147+
"X-UiPath-LlmGateway-ApiFlavor": "chat-completions",
148+
"X-UiPath-LlmGateway-ApiVersion": "2024-10-21",
129149
"X-UiPath-LlmGateway-TimeoutSeconds": "60",
130-
"Authorization": "Bearer " + self.token,
150+
"X-UIPATH-STREAMING-ENABLED": "false",
131151
}
132152

133153
# print(f"Input: {input}")
@@ -226,4 +246,6 @@ def config(self) -> LLMConfig:
226246
model_name=self._model_version,
227247
temperature=self._temperature,
228248
api_key=self._api_key,
249+
api_base=self._endpoint,
250+
api_version=None,
229251
)

deployment/docker_compose/docker-compose.dev.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ services:
4242
- GEN_AI_IDENTITY_ENDPOINT=https://alpha.uipath.com/identity_/connect/token
4343
- GEN_AI_CLIENT_ID=${GEN_AI_CLIENT_ID:-}
4444
- GEN_AI_CLIENT_SECRET=${GEN_AI_CLIENT_SECRET:-}
45+
- GEN_AI_ACCOUNT_ID=${GEN_AI_ACCOUNT_ID:-}
46+
- GEN_AI_TENANT_ID=${GEN_AI_TENANT_ID:-}
4547
- GEN_AI_API_VERSION=${GEN_AI_API_VERSION:-}
4648
- GEN_AI_LLM_PROVIDER_TYPE=${GEN_AI_LLM_PROVIDER_TYPE:-}
4749
- GEN_AI_MAX_TOKENS=${GEN_AI_MAX_TOKENS:-}
@@ -129,6 +131,8 @@ services:
129131
- GEN_AI_IDENTITY_ENDPOINT=https://alpha.uipath.com/identity_/connect/token
130132
- GEN_AI_CLIENT_ID=${GEN_AI_CLIENT_ID:-}
131133
- GEN_AI_CLIENT_SECRET=${GEN_AI_CLIENT_SECRET:-}
134+
- GEN_AI_ACCOUNT_ID=${GEN_AI_ACCOUNT_ID:-}
135+
- GEN_AI_TENANT_ID=${GEN_AI_TENANT_ID:-}
132136
- GEN_AI_API_VERSION=${GEN_AI_API_VERSION:-}
133137
- GEN_AI_LLM_PROVIDER_TYPE=${GEN_AI_LLM_PROVIDER_TYPE:-}
134138
- GEN_AI_MAX_TOKENS=${GEN_AI_MAX_TOKENS:-}

deployment/docker_compose/docker-compose.local.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ services:
4343
- GEN_AI_IDENTITY_ENDPOINT=https://alpha.uipath.com/identity_/connect/token
4444
- GEN_AI_CLIENT_ID=${GEN_AI_CLIENT_ID:-}
4545
- GEN_AI_CLIENT_SECRET=${GEN_AI_CLIENT_SECRET:-}
46+
- GEN_AI_ACCOUNT_ID=${GEN_AI_ACCOUNT_ID:-}
47+
- GEN_AI_TENANT_ID=${GEN_AI_TENANT_ID:-}
4648
- GEN_AI_API_VERSION=${GEN_AI_API_VERSION:-}
4749
- GEN_AI_LLM_PROVIDER_TYPE=${GEN_AI_LLM_PROVIDER_TYPE:-}
4850
- GEN_AI_MAX_TOKENS=${GEN_AI_MAX_TOKENS:-}
@@ -132,6 +134,8 @@ services:
132134
- GEN_AI_IDENTITY_ENDPOINT=https://alpha.uipath.com/identity_/connect/token
133135
- GEN_AI_CLIENT_ID=${GEN_AI_CLIENT_ID:-}
134136
- GEN_AI_CLIENT_SECRET=${GEN_AI_CLIENT_SECRET:-}
137+
- GEN_AI_ACCOUNT_ID=${GEN_AI_ACCOUNT_ID:-}
138+
- GEN_AI_TENANT_ID=${GEN_AI_TENANT_ID:-}
135139
- GEN_AI_API_VERSION=${GEN_AI_API_VERSION:-}
136140
- GEN_AI_LLM_PROVIDER_TYPE=${GEN_AI_LLM_PROVIDER_TYPE:-}
137141
- GEN_AI_MAX_TOKENS=${GEN_AI_MAX_TOKENS:-}

0 commit comments

Comments
 (0)