lambda-erp 1.4.40__tar.gz → 1.4.42__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/PKG-INFO +1 -1
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/chat.py +6 -6
- lambda_erp-1.4.42/api/llm_provider.py +153 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/providers.py +132 -13
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/pyproject.toml +1 -1
- lambda_erp-1.4.40/api/llm_provider.py +0 -110
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/.gitignore +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/LICENSE +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/README.md +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/__init__.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/attachments.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/auth.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/bootstrap.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/chat_links.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/demo_limits.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/deps.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/erp_context.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/errors.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/list_columns.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/list_values.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/mail.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/main.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/mcp_oauth.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/mentions.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/menu_layout.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/number_series.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/oauth.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/order_actions.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/pdf.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/pdf_contract.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/pdf_exports.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/pdf_fonts.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/pdf_pages.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/pdf_profiles.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/pdf_templates.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/pricing_actions.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/record_attachments.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/record_notes.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/remarks_md.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/routers/__init__.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/routers/accounting.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/routers/actions.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/routers/admin.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/routers/analytics.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/routers/availability.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/routers/bank_reconciliation.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/routers/bank_statements.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/routers/chat_api.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/routers/documents.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/routers/masters.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/routers/mcp.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/routers/pdf_templates.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/routers/proposals.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/routers/reports.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/routers/setup.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/services.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/stock_actions.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/templates/_proposal_totals.html +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/templates/_record_base.html +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/templates/delivery_note.html +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/templates/document.html +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/templates/proposal.html +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/templates/record.html +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/templates/reservation.html +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/time_filters.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/tool_permissions.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/tool_results.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/api/write_context.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/docs/agents/README.md +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/frontend/LICENSE +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/frontend/README.md +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/frontend/src/api/client.ts +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/__init__.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/accounting/__init__.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/accounting/bank_account.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/accounting/bank_reconciliation.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/accounting/bank_statement_import.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/accounting/bank_transaction.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/accounting/budget.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/accounting/camt.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/accounting/chart_of_accounts.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/accounting/general_ledger.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/accounting/journal_entry.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/accounting/payment_entry.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/accounting/pos_invoice.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/accounting/purchase_invoice.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/accounting/revaluation.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/accounting/sales_invoice.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/accounting/settlement.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/accounting/setup/__init__.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/accounting/setup/engine.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/accounting/setup/pack.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/accounting/setup/packs/__init__.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/accounting/setup/packs/ch.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/accounting/setup/packs/de_common.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/accounting/setup/packs/de_skr03.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/accounting/setup/packs/de_skr04.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/accounting/setup/packs/generic.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/accounting/setup/profiles.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/accounting/setup/spine.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/accounting/subscription.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/addresses.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/assets/__init__.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/assets/asset.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/assets/lifecycle.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/assets/reservation.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/backlog.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/bundles.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/buying/__init__.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/buying/purchase_order.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/controllers/__init__.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/controllers/currency.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/controllers/defaults.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/controllers/item_price.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/controllers/item_prices.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/controllers/price_resolution.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/controllers/pricing_rule.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/controllers/tax_defaults.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/controllers/taxes_and_totals.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/customer_terms.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/database.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/exceptions.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/hooks.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/master_integrity.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/model.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/order_closing.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/permissions.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/selling/__init__.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/selling/proposal.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/selling/quotation.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/selling/sales_order.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/simulation.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/stock/__init__.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/stock/consignment.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/stock/delivery_note.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/stock/purchase_receipt.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/stock/reservation.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/stock/serial.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/stock/stock_count.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/stock/stock_entry.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/stock/stock_ledger.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/timestamps.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/units.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/utils.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/validation.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/lambda_erp/workflow.py +0 -0
- {lambda_erp-1.4.40 → lambda_erp-1.4.42}/terraform/README.md +0 -0
|
@@ -40,7 +40,7 @@ from api.demo_limits import (
|
|
|
40
40
|
is_demo_role,
|
|
41
41
|
limiter as demo_limiter,
|
|
42
42
|
)
|
|
43
|
-
from api.llm_provider import client_kwargs, llm_configured, not_configured_message
|
|
43
|
+
from api.llm_provider import client_kwargs, llm_configured, llm_settings, not_configured_message
|
|
44
44
|
from api.providers import cost_of_openai_call, cost_of_transcription
|
|
45
45
|
from api.routers.masters import create_master_record, update_master_record, delete_master_record
|
|
46
46
|
from api.routers.analytics import DATASET_FILTER_DESCRIPTION
|
|
@@ -3828,7 +3828,7 @@ def _generate_report_spec_via_openai(
|
|
|
3828
3828
|
actual_cost_usd=cost_of_openai_call(model, usage),
|
|
3829
3829
|
ip=client_ip or "unknown",
|
|
3830
3830
|
role=user_role,
|
|
3831
|
-
provider=
|
|
3831
|
+
provider=llm_settings().provider,
|
|
3832
3832
|
model=model,
|
|
3833
3833
|
prompt_tokens=int(getattr(usage, "input_tokens", 0) or 0) if usage else 0,
|
|
3834
3834
|
completion_tokens=int(getattr(usage, "output_tokens", 0) or 0) if usage else 0,
|
|
@@ -3938,7 +3938,7 @@ async def generate_title(
|
|
|
3938
3938
|
actual_cost_usd=cost_of_openai_call(title_model, usage),
|
|
3939
3939
|
ip=client_ip or "unknown",
|
|
3940
3940
|
role=user_role,
|
|
3941
|
-
provider=
|
|
3941
|
+
provider=llm_settings().provider,
|
|
3942
3942
|
model=title_model,
|
|
3943
3943
|
prompt_tokens=int(getattr(usage, "input_tokens", 0) or 0) if usage else 0,
|
|
3944
3944
|
completion_tokens=int(getattr(usage, "output_tokens", 0) or 0) if usage else 0,
|
|
@@ -4424,7 +4424,7 @@ async def run_thinking_loop(
|
|
|
4424
4424
|
actual_cost_usd=cost_of_openai_call(model_name, usage),
|
|
4425
4425
|
ip=client_ip or "unknown",
|
|
4426
4426
|
role=user_role,
|
|
4427
|
-
provider=
|
|
4427
|
+
provider=llm_settings().provider,
|
|
4428
4428
|
model=model_name,
|
|
4429
4429
|
prompt_tokens=int(getattr(usage, "prompt_tokens", 0) or 0) if usage else 0,
|
|
4430
4430
|
completion_tokens=int(getattr(usage, "completion_tokens", 0) or 0) if usage else 0,
|
|
@@ -4603,7 +4603,7 @@ async def run_thinking_loop(
|
|
|
4603
4603
|
)
|
|
4604
4604
|
demo_limiter.settle(
|
|
4605
4605
|
None, actual_cost_usd=cost_of_openai_call(_chat_model(), usage),
|
|
4606
|
-
ip=client_ip or "unknown", role=user_role, provider=
|
|
4606
|
+
ip=client_ip or "unknown", role=user_role, provider=llm_settings().provider,
|
|
4607
4607
|
model=_chat_model(),
|
|
4608
4608
|
prompt_tokens=int(getattr(usage, "prompt_tokens", 0) or 0) if usage else 0,
|
|
4609
4609
|
completion_tokens=int(getattr(usage, "completion_tokens", 0) or 0) if usage else 0,
|
|
@@ -5219,7 +5219,7 @@ async def chat_websocket(
|
|
|
5219
5219
|
client_ip or "unknown",
|
|
5220
5220
|
cost_of_transcription(TRANSCRIPTION_MODEL, audio_seconds),
|
|
5221
5221
|
role=ws_user_role,
|
|
5222
|
-
provider=
|
|
5222
|
+
provider=llm_settings().provider,
|
|
5223
5223
|
model=TRANSCRIPTION_MODEL,
|
|
5224
5224
|
session_id=data.get("session_id"),
|
|
5225
5225
|
)
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"""Where the ERP's model calls go.
|
|
2
|
+
|
|
3
|
+
LAMBDA_ERP_LLM_PROVIDER=openai (default) OpenAI directly with OPENAI_API_KEY.
|
|
4
|
+
OPENAI_BASE_URL is honoured by the SDK as
|
|
5
|
+
before, for any OpenAI-compatible endpoint.
|
|
6
|
+
LAMBDA_ERP_LLM_PROVIDER=lambda Lambda's model gateway (lambda.dev): the
|
|
7
|
+
same OpenAI API, billed to the Lambda
|
|
8
|
+
organization the key belongs to.
|
|
9
|
+
LAMBDA_LLM_API_KEY a Lambda model key
|
|
10
|
+
(scope llm:erp)
|
|
11
|
+
LAMBDA_LLM_BASE_URL default
|
|
12
|
+
https://platform.lambda.dev/v1/llm/openai
|
|
13
|
+
LAMBDA_ERP_LLM_PROVIDER=azure_foundry Customer-owned Azure AI Foundry (AIServices)
|
|
14
|
+
account via the OpenAI-compatible v1 endpoint
|
|
15
|
+
(https://<account>.openai.azure.com/openai/v1/).
|
|
16
|
+
AZURE_FOUNDRY_API_KEY account key
|
|
17
|
+
AZURE_FOUNDRY_BASE_URL v1 endpoint URL (required)
|
|
18
|
+
AZURE_FOUNDRY_DATA_ZONE global | eu | us (default global)
|
|
19
|
+
|
|
20
|
+
The gateway relays what the ERP uses: Responses (chat, report specialist,
|
|
21
|
+
titles), file uploads for attachments and voice transcription. Chat
|
|
22
|
+
Completions (ERP_CHAT_API=chat) is not available through it. With the lambda
|
|
23
|
+
provider each call names the ERP user (``X-Lambda-ERP-User``, the login name),
|
|
24
|
+
so Lambda's usage view can split the organization's cost by person; OpenAI
|
|
25
|
+
never sees that header.
|
|
26
|
+
|
|
27
|
+
Every OpenAI client in the ERP is built from ``client_kwargs()``.
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
from __future__ import annotations
|
|
31
|
+
|
|
32
|
+
import os
|
|
33
|
+
from dataclasses import dataclass
|
|
34
|
+
from typing import Optional
|
|
35
|
+
|
|
36
|
+
import httpx
|
|
37
|
+
from openai import OpenAI
|
|
38
|
+
|
|
39
|
+
DEFAULT_LAMBDA_BASE_URL = "https://platform.lambda.dev/v1/llm/openai"
|
|
40
|
+
ERP_USER_HEADER = "X-Lambda-ERP-User"
|
|
41
|
+
_PLACEHOLDERS = {"", "sk-your-key-here"}
|
|
42
|
+
_AZURE_FOUNDRY_PRICING_TABLES = {
|
|
43
|
+
"global": "openai",
|
|
44
|
+
"eu": "azure_foundry_eu",
|
|
45
|
+
"us": "azure_foundry_us",
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@dataclass(frozen=True)
|
|
50
|
+
class LLMSettings:
|
|
51
|
+
provider: str # "openai", "lambda", or "azure_foundry"
|
|
52
|
+
api_key: str
|
|
53
|
+
base_url: Optional[str] # None: the SDK's default (or OPENAI_BASE_URL)
|
|
54
|
+
key_variable: str # the env variable the key comes from, for messages
|
|
55
|
+
pricing_table: str = "openai" # "openai", "azure_foundry_eu", or "azure_foundry_us"
|
|
56
|
+
|
|
57
|
+
@property
|
|
58
|
+
def configured(self) -> bool:
|
|
59
|
+
if self.api_key.strip() in _PLACEHOLDERS:
|
|
60
|
+
return False
|
|
61
|
+
if self.provider == "azure_foundry" and not self.base_url:
|
|
62
|
+
return False
|
|
63
|
+
return True
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def llm_settings() -> LLMSettings:
|
|
67
|
+
"""Read on every call, so a changed environment applies without a restart."""
|
|
68
|
+
provider = (os.environ.get("LAMBDA_ERP_LLM_PROVIDER") or "openai").strip().lower()
|
|
69
|
+
if provider == "lambda":
|
|
70
|
+
return LLMSettings(
|
|
71
|
+
provider="lambda",
|
|
72
|
+
api_key=os.environ.get("LAMBDA_LLM_API_KEY", ""),
|
|
73
|
+
base_url=(os.environ.get("LAMBDA_LLM_BASE_URL") or DEFAULT_LAMBDA_BASE_URL).rstrip("/"),
|
|
74
|
+
key_variable="LAMBDA_LLM_API_KEY",
|
|
75
|
+
pricing_table="openai",
|
|
76
|
+
)
|
|
77
|
+
if provider == "azure_foundry":
|
|
78
|
+
zone = (os.environ.get("AZURE_FOUNDRY_DATA_ZONE") or "global").strip().lower()
|
|
79
|
+
pricing_table = _AZURE_FOUNDRY_PRICING_TABLES.get(zone)
|
|
80
|
+
if pricing_table is None:
|
|
81
|
+
raise RuntimeError(f"AZURE_FOUNDRY_DATA_ZONE={zone!r}: use 'global', 'eu', or 'us'")
|
|
82
|
+
base_url = (os.environ.get("AZURE_FOUNDRY_BASE_URL") or "").strip().rstrip("/") or None
|
|
83
|
+
return LLMSettings(
|
|
84
|
+
provider="azure_foundry",
|
|
85
|
+
api_key=os.environ.get("AZURE_FOUNDRY_API_KEY", ""),
|
|
86
|
+
base_url=base_url,
|
|
87
|
+
key_variable="AZURE_FOUNDRY_API_KEY",
|
|
88
|
+
pricing_table=pricing_table,
|
|
89
|
+
)
|
|
90
|
+
if provider != "openai":
|
|
91
|
+
raise RuntimeError(
|
|
92
|
+
f"LAMBDA_ERP_LLM_PROVIDER={provider!r}: use 'openai', 'lambda', or 'azure_foundry'"
|
|
93
|
+
)
|
|
94
|
+
return LLMSettings(
|
|
95
|
+
provider="openai",
|
|
96
|
+
api_key=os.environ.get("OPENAI_API_KEY", ""),
|
|
97
|
+
base_url=None,
|
|
98
|
+
key_variable="OPENAI_API_KEY",
|
|
99
|
+
pricing_table="openai",
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def llm_configured() -> bool:
|
|
104
|
+
try:
|
|
105
|
+
return llm_settings().configured
|
|
106
|
+
except RuntimeError:
|
|
107
|
+
return False
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def not_configured_message() -> str:
|
|
111
|
+
try:
|
|
112
|
+
settings = llm_settings()
|
|
113
|
+
except RuntimeError as exc:
|
|
114
|
+
return str(exc)
|
|
115
|
+
if settings.api_key.strip() in _PLACEHOLDERS:
|
|
116
|
+
return f"{settings.key_variable} is not configured"
|
|
117
|
+
if settings.provider == "azure_foundry" and not settings.base_url:
|
|
118
|
+
return "AZURE_FOUNDRY_BASE_URL is not configured"
|
|
119
|
+
return f"{settings.key_variable} is not configured"
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _client_timeout(timeout):
|
|
123
|
+
"""The timeout in the type the installed OpenAI SDK expects.
|
|
124
|
+
|
|
125
|
+
Since openai 3.x the SDK runs on its own HTTP client (httpx2), and httpx2
|
|
126
|
+
2.17 rejects an ``httpx.Timeout``. ``openai.Timeout`` is always the SDK's
|
|
127
|
+
own type, so convert instead of depending on which httpx the SDK uses.
|
|
128
|
+
"""
|
|
129
|
+
import openai
|
|
130
|
+
if timeout is None or isinstance(timeout, (int, float, openai.Timeout)):
|
|
131
|
+
return timeout
|
|
132
|
+
return openai.Timeout(connect=timeout.connect, read=timeout.read, write=timeout.write, pool=timeout.pool)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def client_kwargs(timeout: httpx.Timeout, erp_user: Optional[str] = None) -> dict:
|
|
136
|
+
"""Keyword arguments for ``OpenAI(...)`` with the configured provider."""
|
|
137
|
+
settings = llm_settings()
|
|
138
|
+
if settings.provider == "azure_foundry" and not settings.base_url:
|
|
139
|
+
raise RuntimeError("AZURE_FOUNDRY_BASE_URL is not configured")
|
|
140
|
+
headers = {}
|
|
141
|
+
if settings.provider == "lambda" and erp_user:
|
|
142
|
+
headers[ERP_USER_HEADER] = str(erp_user)[:100]
|
|
143
|
+
kwargs = {"api_key": settings.api_key, "timeout": _client_timeout(timeout)}
|
|
144
|
+
if settings.base_url:
|
|
145
|
+
kwargs["base_url"] = settings.base_url
|
|
146
|
+
if headers:
|
|
147
|
+
kwargs["default_headers"] = headers
|
|
148
|
+
return kwargs
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def openai_client(timeout: httpx.Timeout, erp_user: Optional[str] = None) -> OpenAI:
|
|
152
|
+
"""An OpenAI client for the configured provider."""
|
|
153
|
+
return OpenAI(**client_kwargs(timeout, erp_user))
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"""Pricing data for external LLM APIs the chat orchestrator uses.
|
|
2
2
|
|
|
3
|
-
Mirrors the shape of `lambda-web/backend/providers.py`. Lists the OpenAI
|
|
4
|
-
|
|
5
|
-
see docs/llm-pricing.md for sources and billing scope.
|
|
3
|
+
Mirrors the shape of `lambda-web/backend/providers.py`. Lists the OpenAI
|
|
4
|
+
Standard/Global rates as well as verified Azure AI Foundry EU and US Data Zone
|
|
5
|
+
rates (USD per 1M tokens); see docs/llm-pricing.md for sources and billing scope.
|
|
6
6
|
|
|
7
7
|
Consumers import `cost_of_openai_call` to turn an SDK `response.usage` object
|
|
8
8
|
into a USD amount. Both Chat Completions and Responses usage shapes are
|
|
@@ -15,7 +15,7 @@ from typing import Any, Optional
|
|
|
15
15
|
|
|
16
16
|
|
|
17
17
|
# ---------------------------------------------------------------------------
|
|
18
|
-
# Pricing dicts
|
|
18
|
+
# Pricing dicts (USD per 1M tokens)
|
|
19
19
|
# ---------------------------------------------------------------------------
|
|
20
20
|
|
|
21
21
|
OPENAI_PRICING: dict[str, dict[str, Any]] = {
|
|
@@ -69,6 +69,100 @@ OPENAI_PRICING: dict[str, dict[str, Any]] = {
|
|
|
69
69
|
},
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
+
# Verified live from Azure Retail Prices API
|
|
73
|
+
# (https://prices.azure.com/api/retail/prices, serviceName eq 'Foundry Models',
|
|
74
|
+
# productName eq 'Azure OpenAI GPT6', Std DZ) on 2026-12-02.
|
|
75
|
+
# EU Data Zone (switzerlandnorth):
|
|
76
|
+
AZURE_FOUNDRY_EU_PRICING: dict[str, dict[str, Any]] = {
|
|
77
|
+
"gpt-6.2-astra": {
|
|
78
|
+
"tiered": True,
|
|
79
|
+
"tiers": [
|
|
80
|
+
{"max_input_tokens": 272_000, "input": 9.60, "cached_input": 0.48, "cache_write": 12.00, "cache_write_12h": 24.00, "output": 48.00},
|
|
81
|
+
{"max_input_tokens": None, "input": 19.20, "cached_input": 0.96, "cache_write": 24.00, "cache_write_12h": 48.00, "output": 72.00},
|
|
82
|
+
],
|
|
83
|
+
},
|
|
84
|
+
"gpt-6.2-sol": {
|
|
85
|
+
"tiered": True,
|
|
86
|
+
"tiers": [
|
|
87
|
+
{"max_input_tokens": 272_000, "input": 2.40, "cached_input": 0.12, "cache_write": 3.00, "cache_write_12h": 6.00, "output": 12.00},
|
|
88
|
+
{"max_input_tokens": None, "input": 4.80, "cached_input": 0.24, "cache_write": 6.00, "cache_write_12h": 12.00, "output": 18.00},
|
|
89
|
+
],
|
|
90
|
+
},
|
|
91
|
+
"gpt-6-astra": {
|
|
92
|
+
"tiered": True,
|
|
93
|
+
"tiers": [
|
|
94
|
+
{"max_input_tokens": 272_000, "input": 12.00, "cached_input": 1.20, "cache_write": 15.00, "output": 60.00},
|
|
95
|
+
{"max_input_tokens": None, "input": 24.00, "cached_input": 2.40, "cache_write": 30.00, "output": 90.00},
|
|
96
|
+
],
|
|
97
|
+
},
|
|
98
|
+
"gpt-6.1-sol": {
|
|
99
|
+
"tiered": True,
|
|
100
|
+
"tiers": [
|
|
101
|
+
{"max_input_tokens": 272_000, "input": 2.40, "cached_input": 0.12, "cache_write": 3.00, "output": 12.00},
|
|
102
|
+
{"max_input_tokens": None, "input": 4.80, "cached_input": 0.24, "cache_write": 6.00, "output": 18.00},
|
|
103
|
+
],
|
|
104
|
+
},
|
|
105
|
+
"gpt-6-luna": {
|
|
106
|
+
"tiered": True,
|
|
107
|
+
"tiers": [
|
|
108
|
+
{"max_input_tokens": 272_000, "input": 0.12, "cached_input": 0.012, "cache_write": 0.15, "output": 0.60},
|
|
109
|
+
{"max_input_tokens": None, "input": 0.24, "cached_input": 0.024, "cache_write": 0.30, "output": 0.90},
|
|
110
|
+
],
|
|
111
|
+
},
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
# US Data Zone (eastus2), verified 2026-12-02:
|
|
115
|
+
AZURE_FOUNDRY_US_PRICING: dict[str, dict[str, Any]] = {
|
|
116
|
+
"gpt-6.2-astra": {
|
|
117
|
+
"tiered": True,
|
|
118
|
+
"tiers": [
|
|
119
|
+
{"max_input_tokens": 272_000, "input": 8.80, "cached_input": 0.44, "cache_write": 11.00, "cache_write_12h": 22.00, "output": 44.00},
|
|
120
|
+
{"max_input_tokens": None, "input": 17.60, "cached_input": 0.88, "cache_write": 22.00, "cache_write_12h": 44.00, "output": 66.00},
|
|
121
|
+
],
|
|
122
|
+
},
|
|
123
|
+
"gpt-6.2-sol": {
|
|
124
|
+
"tiered": True,
|
|
125
|
+
"tiers": [
|
|
126
|
+
{"max_input_tokens": 272_000, "input": 2.20, "cached_input": 0.11, "cache_write": 2.75, "cache_write_12h": 5.50, "output": 11.00},
|
|
127
|
+
{"max_input_tokens": None, "input": 4.40, "cached_input": 0.22, "cache_write": 5.50, "cache_write_12h": 11.00, "output": 16.50},
|
|
128
|
+
],
|
|
129
|
+
},
|
|
130
|
+
"gpt-6-astra": {
|
|
131
|
+
"tiered": True,
|
|
132
|
+
"tiers": [
|
|
133
|
+
{"max_input_tokens": 272_000, "input": 11.00, "cached_input": 1.10, "cache_write": 13.75, "output": 55.00},
|
|
134
|
+
{"max_input_tokens": None, "input": 22.00, "cached_input": 2.20, "cache_write": 27.50, "output": 82.50},
|
|
135
|
+
],
|
|
136
|
+
},
|
|
137
|
+
"gpt-6.1-sol": {
|
|
138
|
+
"tiered": True,
|
|
139
|
+
"tiers": [
|
|
140
|
+
{"max_input_tokens": 272_000, "input": 2.20, "cached_input": 0.11, "cache_write": 2.75, "output": 11.00},
|
|
141
|
+
{"max_input_tokens": None, "input": 4.40, "cached_input": 0.22, "cache_write": 5.50, "output": 16.50},
|
|
142
|
+
],
|
|
143
|
+
},
|
|
144
|
+
"gpt-6.1-terra": {
|
|
145
|
+
"tiered": True,
|
|
146
|
+
"tiers": [
|
|
147
|
+
{"max_input_tokens": 272_000, "input": 1.10, "cached_input": 0.055, "cache_write": 1.375, "output": 5.50},
|
|
148
|
+
{"max_input_tokens": None, "input": 2.20, "cached_input": 0.11, "cache_write": 2.75, "output": 8.25},
|
|
149
|
+
],
|
|
150
|
+
},
|
|
151
|
+
"gpt-6-luna": {
|
|
152
|
+
"tiered": True,
|
|
153
|
+
"tiers": [
|
|
154
|
+
{"max_input_tokens": 272_000, "input": 0.11, "cached_input": 0.011, "cache_write": 0.1375, "output": 0.55},
|
|
155
|
+
{"max_input_tokens": None, "input": 0.22, "cached_input": 0.022, "cache_write": 0.275, "output": 0.825},
|
|
156
|
+
],
|
|
157
|
+
},
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
PRICING_TABLES: dict[str, dict[str, dict[str, Any]]] = {
|
|
161
|
+
"openai": OPENAI_PRICING,
|
|
162
|
+
"azure_foundry_eu": AZURE_FOUNDRY_EU_PRICING,
|
|
163
|
+
"azure_foundry_us": AZURE_FOUNDRY_US_PRICING,
|
|
164
|
+
}
|
|
165
|
+
|
|
72
166
|
# Speech-to-text, priced per minute of audio (not per token).
|
|
73
167
|
TRANSCRIBE_PRICING: dict[str, dict[str, float]] = {
|
|
74
168
|
"gpt-transcribe": {"per_minute": 0.0045},
|
|
@@ -79,14 +173,31 @@ TRANSCRIBE_PRICING: dict[str, dict[str, float]] = {
|
|
|
79
173
|
|
|
80
174
|
|
|
81
175
|
# ---------------------------------------------------------------------------
|
|
82
|
-
# Rate lookup — unknown model IDs are estimated at the gpt-6.1-sol rates
|
|
83
|
-
# Register a model explicitly before using
|
|
176
|
+
# Rate lookup — unknown model IDs are estimated at the gpt-6.1-sol rates
|
|
177
|
+
# within the selected pricing table. Register a model explicitly before using
|
|
178
|
+
# it; this fallback is not a price cap.
|
|
84
179
|
# ---------------------------------------------------------------------------
|
|
85
180
|
|
|
86
|
-
def
|
|
87
|
-
|
|
181
|
+
def _resolve_pricing_table(pricing_table: Optional[str]) -> dict[str, dict[str, Any]]:
|
|
182
|
+
if pricing_table is None:
|
|
183
|
+
from api.llm_provider import llm_settings
|
|
184
|
+
try:
|
|
185
|
+
pricing_table = llm_settings().pricing_table
|
|
186
|
+
except RuntimeError:
|
|
187
|
+
pricing_table = "openai"
|
|
188
|
+
return PRICING_TABLES.get(pricing_table, OPENAI_PRICING)
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def get_openai_rates(
|
|
192
|
+
model: str,
|
|
193
|
+
input_tokens: int = 0,
|
|
194
|
+
*,
|
|
195
|
+
pricing_table: Optional[str] = None,
|
|
196
|
+
) -> dict[str, float]:
|
|
197
|
+
table = _resolve_pricing_table(pricing_table)
|
|
198
|
+
entry = table.get(model)
|
|
88
199
|
if entry is None:
|
|
89
|
-
entry =
|
|
200
|
+
entry = table["gpt-6.1-sol"]
|
|
90
201
|
if entry.get("tiered"):
|
|
91
202
|
for tier in entry["tiers"]:
|
|
92
203
|
cap = tier.get("max_input_tokens")
|
|
@@ -101,11 +212,17 @@ def get_openai_rates(model: str, input_tokens: int = 0) -> dict[str, float]:
|
|
|
101
212
|
# and degrade to 0 so callers don't need to pre-check.
|
|
102
213
|
# ---------------------------------------------------------------------------
|
|
103
214
|
|
|
104
|
-
def cost_of_openai_call(
|
|
105
|
-
|
|
215
|
+
def cost_of_openai_call(
|
|
216
|
+
model: str,
|
|
217
|
+
usage: Optional[Any],
|
|
218
|
+
*,
|
|
219
|
+
pricing_table: Optional[str] = None,
|
|
220
|
+
) -> float:
|
|
221
|
+
"""USD cost of one OpenAI-compatible Chat Completions or Responses API call.
|
|
106
222
|
|
|
107
223
|
Understands either endpoint's cached-token breakdown so cache hits are
|
|
108
|
-
billed at the discounted rate.
|
|
224
|
+
billed at the discounted rate. Uses the active provider's pricing table
|
|
225
|
+
when ``pricing_table`` is not passed."""
|
|
109
226
|
if usage is None:
|
|
110
227
|
return 0.0
|
|
111
228
|
prompt_tokens = int(
|
|
@@ -127,7 +244,7 @@ def cost_of_openai_call(model: str, usage: Optional[Any]) -> float:
|
|
|
127
244
|
if details is not None:
|
|
128
245
|
cached = int(getattr(details, "cached_tokens", 0) or 0)
|
|
129
246
|
|
|
130
|
-
rates = get_openai_rates(model, input_tokens=prompt_tokens)
|
|
247
|
+
rates = get_openai_rates(model, input_tokens=prompt_tokens, pricing_table=pricing_table)
|
|
131
248
|
uncached = max(prompt_tokens - cached, 0)
|
|
132
249
|
cached_rate = rates.get("cached_input", rates["input"])
|
|
133
250
|
total = (
|
|
@@ -136,6 +253,8 @@ def cost_of_openai_call(model: str, usage: Optional[Any]) -> float:
|
|
|
136
253
|
+ completion_tokens * rates["output"]
|
|
137
254
|
)
|
|
138
255
|
return total / 1_000_000
|
|
256
|
+
|
|
257
|
+
|
|
139
258
|
def cost_of_transcription(model: str, audio_seconds: float) -> float:
|
|
140
259
|
"""USD cost of one speech-to-text call, billed per minute of audio.
|
|
141
260
|
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
"""Where the ERP's model calls go.
|
|
2
|
-
|
|
3
|
-
LAMBDA_ERP_LLM_PROVIDER=openai (default) OpenAI directly with OPENAI_API_KEY.
|
|
4
|
-
OPENAI_BASE_URL is honoured by the SDK as
|
|
5
|
-
before, for any OpenAI-compatible endpoint.
|
|
6
|
-
LAMBDA_ERP_LLM_PROVIDER=lambda Lambda's model gateway (lambda.dev): the
|
|
7
|
-
same OpenAI API, billed to the Lambda
|
|
8
|
-
organization the key belongs to.
|
|
9
|
-
LAMBDA_LLM_API_KEY a Lambda model key
|
|
10
|
-
(scope llm:erp)
|
|
11
|
-
LAMBDA_LLM_BASE_URL default
|
|
12
|
-
https://platform.lambda.dev/v1/llm/openai
|
|
13
|
-
|
|
14
|
-
The gateway relays what the ERP uses: Responses (chat, report specialist,
|
|
15
|
-
titles), file uploads for attachments and voice transcription. Chat
|
|
16
|
-
Completions (ERP_CHAT_API=chat) is not available through it. With the lambda
|
|
17
|
-
provider each call names the ERP user (``X-Lambda-ERP-User``, the login name),
|
|
18
|
-
so Lambda's usage view can split the organization's cost by person; OpenAI
|
|
19
|
-
never sees that header.
|
|
20
|
-
|
|
21
|
-
Every OpenAI client in the ERP is built from ``client_kwargs()``.
|
|
22
|
-
"""
|
|
23
|
-
|
|
24
|
-
from __future__ import annotations
|
|
25
|
-
|
|
26
|
-
import os
|
|
27
|
-
from dataclasses import dataclass
|
|
28
|
-
from typing import Optional
|
|
29
|
-
|
|
30
|
-
import httpx
|
|
31
|
-
from openai import OpenAI
|
|
32
|
-
|
|
33
|
-
DEFAULT_LAMBDA_BASE_URL = "https://platform.lambda.dev/v1/llm/openai"
|
|
34
|
-
ERP_USER_HEADER = "X-Lambda-ERP-User"
|
|
35
|
-
_PLACEHOLDERS = {"", "sk-your-key-here"}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
@dataclass(frozen=True)
|
|
39
|
-
class LLMSettings:
|
|
40
|
-
provider: str # "openai" or "lambda"
|
|
41
|
-
api_key: str
|
|
42
|
-
base_url: Optional[str] # None: the SDK's default (or OPENAI_BASE_URL)
|
|
43
|
-
key_variable: str # the env variable the key comes from, for messages
|
|
44
|
-
|
|
45
|
-
@property
|
|
46
|
-
def configured(self) -> bool:
|
|
47
|
-
return self.api_key.strip() not in _PLACEHOLDERS
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
def llm_settings() -> LLMSettings:
|
|
51
|
-
"""Read on every call, so a changed environment applies without a restart."""
|
|
52
|
-
provider = (os.environ.get("LAMBDA_ERP_LLM_PROVIDER") or "openai").strip().lower()
|
|
53
|
-
if provider == "lambda":
|
|
54
|
-
return LLMSettings(
|
|
55
|
-
provider="lambda",
|
|
56
|
-
api_key=os.environ.get("LAMBDA_LLM_API_KEY", ""),
|
|
57
|
-
base_url=(os.environ.get("LAMBDA_LLM_BASE_URL") or DEFAULT_LAMBDA_BASE_URL).rstrip("/"),
|
|
58
|
-
key_variable="LAMBDA_LLM_API_KEY",
|
|
59
|
-
)
|
|
60
|
-
if provider != "openai":
|
|
61
|
-
raise RuntimeError(f"LAMBDA_ERP_LLM_PROVIDER={provider!r}: use 'openai' or 'lambda'")
|
|
62
|
-
return LLMSettings(provider="openai", api_key=os.environ.get("OPENAI_API_KEY", ""),
|
|
63
|
-
base_url=None, key_variable="OPENAI_API_KEY")
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
def llm_configured() -> bool:
|
|
67
|
-
try:
|
|
68
|
-
return llm_settings().configured
|
|
69
|
-
except RuntimeError:
|
|
70
|
-
return False
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
def not_configured_message() -> str:
|
|
74
|
-
try:
|
|
75
|
-
settings = llm_settings()
|
|
76
|
-
except RuntimeError as exc:
|
|
77
|
-
return str(exc)
|
|
78
|
-
return f"{settings.key_variable} is not configured"
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
def _client_timeout(timeout):
|
|
82
|
-
"""The timeout in the type the installed OpenAI SDK expects.
|
|
83
|
-
|
|
84
|
-
Since openai 3.x the SDK runs on its own HTTP client (httpx2), and httpx2
|
|
85
|
-
2.17 rejects an ``httpx.Timeout``. ``openai.Timeout`` is always the SDK's
|
|
86
|
-
own type, so convert instead of depending on which httpx the SDK uses.
|
|
87
|
-
"""
|
|
88
|
-
import openai
|
|
89
|
-
if timeout is None or isinstance(timeout, (int, float, openai.Timeout)):
|
|
90
|
-
return timeout
|
|
91
|
-
return openai.Timeout(connect=timeout.connect, read=timeout.read, write=timeout.write, pool=timeout.pool)
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
def client_kwargs(timeout: httpx.Timeout, erp_user: Optional[str] = None) -> dict:
|
|
95
|
-
"""Keyword arguments for ``OpenAI(...)`` with the configured provider."""
|
|
96
|
-
settings = llm_settings()
|
|
97
|
-
headers = {}
|
|
98
|
-
if settings.provider == "lambda" and erp_user:
|
|
99
|
-
headers[ERP_USER_HEADER] = str(erp_user)[:100]
|
|
100
|
-
kwargs = {"api_key": settings.api_key, "timeout": _client_timeout(timeout)}
|
|
101
|
-
if settings.base_url:
|
|
102
|
-
kwargs["base_url"] = settings.base_url
|
|
103
|
-
if headers:
|
|
104
|
-
kwargs["default_headers"] = headers
|
|
105
|
-
return kwargs
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
def openai_client(timeout: httpx.Timeout, erp_user: Optional[str] = None) -> OpenAI:
|
|
109
|
-
"""An OpenAI client for the configured provider."""
|
|
110
|
-
return OpenAI(**client_kwargs(timeout, erp_user))
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|