11from openai import OpenAI
22
3- from .._utils .constants import OPENAI_VALUE_ERROR , OPENAI_PROMPT_EMPTY_EXCEPTION
43from . import ILlm
4+ from .._utils .constants import OPENAI_VALUE_ERROR , OPENAI_PROMPT_EMPTY_EXCEPTION
55
66
77class OpenAi (ILlm ):
@@ -16,6 +16,7 @@ def __init__(self, config=None, client=None):
1616 Returns:
1717 None
1818 """
19+ self .config = config
1920 self .client = client
2021
2122 if client is not None :
@@ -24,9 +25,8 @@ def __init__(self, config=None, client=None):
2425
2526 if 'api_key' not in config :
2627 raise ValueError (OPENAI_VALUE_ERROR )
27-
28- if 'api_key' in config :
29- self .client = OpenAI (api_key = config ['api_key' ])
28+ api_key = config .pop ('api_key' )
29+ self .client = OpenAI (api_key = api_key , ** config )
3030
3131 def system_message (self , message : str ) -> any :
3232 """
@@ -82,6 +82,6 @@ def invoke(self, prompt, **kwargs) -> str:
8282 model = self .config .get ("model" , "gpt-3.5-turbo" )
8383 temperature = kwargs .get ("temperature" , 0.1 )
8484 max_tokens = kwargs .get ("max_tokens" , 500 )
85- response = self .client .chat .completions .create (model = model , messages = [{"role" : "user" , "content" : prompt }], max_tokens = max_tokens , stop = None ,
86- temperature = temperature )
85+ response = self .client .chat .completions .create (model = model , messages = [{"role" : "user" , "content" : prompt }],
86+ max_tokens = max_tokens , stop = None , temperature = temperature )
8787 return response .choices [0 ].message .content
0 commit comments