How to call functions with chat models - Utilities
Utilities First let's define a few utilities for making calls to the Chat Completions API and for maintaining and keeping track of the conversation state. @retry(wait=wait_random_exponential(multiplier=1, max=40), stop=stop_after_attempt(3)) def chat_completion_request(messages, functions=None, function_call=None, model=GPT_MODEL): headers = { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + openai.api_key, } json_data = {'model': model, 'messages': messages} if functions is not None: json_data.update({'functions': functions}) if function_call is not None: json_data.update({'function_call': function_call}) try: response = requests.post( 'https://api.openai.com/v1/chat/completions', headers=headers, json=json_data, ) return response except Exception as e: print('Unable to generate ChatCompletion response') print(f'Exception: {e}') return e