Skip to content

Commit

Permalink
feat: add base url for OpenAI connection (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
TrachukT authored Feb 27, 2025
1 parent 2fd7947 commit e9b4e54
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 14 additions & 1 deletion dynamiq/connections/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,10 @@ class OpenAI(BaseApiKeyConnection):
Attributes:
api_key (str): The API key for the OpenAI service, fetched from the environment variable 'OPENAI_API_KEY'.
url (str): The endpoint url for the OpenAI service, fetched from the environment variable 'OPENAI_URL'.
"""
api_key: str = Field(default_factory=partial(get_env_var, "OPENAI_API_KEY"))
url: str = Field(default_factory=partial(get_env_var, "OPENAI_URL", "https://api.openai.com/v1"))

def connect(self) -> "OpenAIClient":
"""
Expand All @@ -201,10 +203,21 @@ def connect(self) -> "OpenAIClient":
"""
# Import in runtime to save memory
from openai import OpenAI as OpenAIClient
openai_client = OpenAIClient(api_key=self.api_key)

openai_client = OpenAIClient(api_key=self.api_key, base_url=self.url)
logger.debug("Connected to OpenAI")
return openai_client

@property
def conn_params(self) -> dict:
"""
Returns the parameters required for connection.
"""
return {
"api_base": self.url,
"api_key": self.api_key,
}


class Anthropic(BaseApiKeyConnection):
api_key: str = Field(default_factory=partial(get_env_var, "ANTHROPIC_API_KEY"))
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/flows/test_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ def test_workflow_with_depend_nodes_with_tracing(
client=ANY,
response_format=None,
drop_params=True,
api_base="https://api.openai.com/v1",
),
mock.call(
tools=None,
Expand Down Expand Up @@ -277,6 +278,7 @@ def test_workflow_with_depend_nodes_and_depend_fail(
top_p=None,
response_format=None,
drop_params=True,
api_base="https://api.openai.com/v1",
)
]

Expand Down

1 comment on commit e9b4e54

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report •
FileStmtsMissCoverMissing
dynamiq/connections
   connections.py4265487%14–18, 58, 66, 77, 100, 477–478, 480–481, 485–486, 488–489, 502–503, 505, 531, 533, 545, 547–549, 623–624, 665, 765, 818, 863–864, 875, 969–970, 1003–1004, 1040–1041, 1090–1091, 1093, 1096–1097, 1102, 1107, 1114–1115, 1118–1122
TOTAL11912363069% 

Tests Skipped Failures Errors Time
416 0 💤 0 ❌ 0 🔥 46.408s ⏱️

Please sign in to comment.