import json from haystack.dataclasses import ChatMessage, ChatRole response = None messages = [ ChatMessage.from_system(context) ] while True: # if OpenAI response is a tool call if response and response["replies"][0].meta["finish_reason"] == "tool_calls": function_calls = json.loads(response["replies"][0].content) for function_call in function_calls: ## Parse function calling information function_name = function_call["function"]["name"] function_args = json.loads(function_call["function"]["arguments"]) ## Find the correspoding function and call it with the given arguments function_to_call = available_functions[function_name] function_response = function_to_call(**function_args) ## Append function response to the messages list using `ChatMessage.from_function` messages.append(ChatMessage.from_function(content=json.dumps(function_response), name=function_name)) # Regular Conversation else: # Append assistant messages to the messages list if not messages[-1].is_from(ChatRole.SYSTEM): messages.append(response["replies"][0]) user_input = input("ENTER YOUR MESSAGE 👇 INFO: Type 'exit' or 'quit' to stopn") if user_input.lower() == "exit" or user_input.lower() == "quit": break else: messages.append(ChatMessage.from_user(user_input)) response = chat_generator.run(messages=messages, generation_kwargs={"tools": tools})