11"""OpenAI API-backed bots
22
3- They can be used with services other than OpenAPI as long as them implement a
3+ They can be used with services other than OpenAI as long as them implement a
44sufficient subset of the API. For example the `completions_bot` only requires
55tools support.
66
1515from collections .abc import Mapping , Sequence
1616import json
1717import logging
18- import os
1918from pathlib import PurePosixPath
2019from typing import Any , Self , TypedDict , override
2120
2221import openai
2322
24- from ..common import JSONObject , reindent
23+ from ..common import JSONObject , config_string , reindent
2524from .common import Action , Bot , Goal , Toolbox
2625
2726
@@ -37,10 +36,7 @@ def completions_bot(
3736 model : str = _DEFAULT_MODEL ,
3837) -> Bot :
3938 """Compatibility-mode bot, uses completions with function calling"""
40- if api_key and api_key .startswith ("$" ):
41- api_key = os .environ [api_key [1 :]]
42- client = openai .OpenAI (api_key = api_key , base_url = base_url )
43- return _CompletionsBot (client , model )
39+ return _CompletionsBot (_new_client (api_key , base_url ), model )
4440
4541
4642def threads_bot (
@@ -49,8 +45,14 @@ def threads_bot(
4945 model : str = _DEFAULT_MODEL ,
5046) -> Bot :
5147 """Beta bot, uses assistant threads with function calling"""
52- client = openai .OpenAI (api_key = api_key , base_url = base_url )
53- return _ThreadsBot .create (client , model )
48+ return _ThreadsBot .create (_new_client (api_key , base_url ), model )
49+
50+
51+ def _new_client (api_key : str | None , base_url : str | None ) -> openai .OpenAI :
52+ return openai .OpenAI (
53+ api_key = config_string (api_key ) if api_key else None ,
54+ base_url = base_url ,
55+ )
5456
5557
5658class _ToolsFactory :
0 commit comments