From 2641e418cd4d5dd393a6780430c2279b0f39641e Mon Sep 17 00:00:00 2001 From: cybermaggedon Date: Thu, 22 May 2025 19:00:29 +0100 Subject: [PATCH] Make sure text-completion-openai works with empty base URL string (#387) --- .../model/text_completion/openai/llm.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/trustgraph-flow/trustgraph/model/text_completion/openai/llm.py b/trustgraph-flow/trustgraph/model/text_completion/openai/llm.py index 590c2e3f..e278a976 100755 --- a/trustgraph-flow/trustgraph/model/text_completion/openai/llm.py +++ b/trustgraph-flow/trustgraph/model/text_completion/openai/llm.py @@ -24,7 +24,10 @@ default_model = 'gpt-3.5-turbo' default_temperature = 0.0 default_max_output = 4096 default_api_key = os.getenv("OPENAI_TOKEN") -default_base_url = os.getenv("OPENAI_BASE_URL", None) +default_base_url = os.getenv("OPENAI_BASE_URL") + +if default_base_url is None or default_base_url == "": + default_base_url = "https://api.openai.com/v1" class Processor(ConsumerProducer): @@ -35,7 +38,7 @@ class Processor(ConsumerProducer): subscriber = params.get("subscriber", default_subscriber) model = params.get("model", default_model) api_key = params.get("api_key", default_api_key) - base_url = params.get("base_url", default_base_url) + base_url = params.get("url", default_base_url) temperature = params.get("temperature", default_temperature) max_output = params.get("max_output", default_max_output) @@ -72,7 +75,11 @@ class Processor(ConsumerProducer): self.model = model self.temperature = temperature self.max_output = max_output - self.openai = OpenAI(base_url=base_url, api_key=api_key) + + if base_url: + self.openai = OpenAI(base_url=base_url, api_key=api_key) + else: + self.openai = OpenAI(api_key=api_key) print("Initialised", flush=True) @@ -144,7 +151,7 @@ class Processor(ConsumerProducer): # Apart from rate limits, treat all exceptions as unrecoverable - print(f"Exception: {e}") + print(f"Exception: {type(e)} {e}") print("Send error response...", flush=True)