Ich habe diese Funktion die einen Query sendet und dabei function calling verwendet.
def frage_gpt_mit_tools(chat_history):
try:
response = openai.ChatCompletion.create(
#model="gpt-4-0613",
#model="gpt-4o",
#model="phi-3.1-mini-128k-instruct",
#model="openai/gpt-oss-20b",
model="qwen/qwen3-4b-2507",
messages=chat_history,
tools=tools,
tool_choice="auto"
)
response_message = response['choices'][0]['message']
tool_calls = response_message.get("tool_calls", [])
if tool_calls:
chat_history.append(response_message) # Nur einmal hinzufügen
for call in tool_calls:
function_name = call['function']['name']
arguments = json.loads(call['function']['arguments'])
if function_name == "list_files":
result = list_files(arguments['directory'])
elif function_name == "read_file":
result = read_file(arguments['path'])
elif function_name == "execute_cmd":
print("CMD: ", arguments['command'])
result = execute_shell_command(arguments['command'])
elif function_name == "execute_powershell":
print("PWSH: ", arguments['command'])
result = execute_powershell_command(arguments['command'])
elif function_name == "fetch_html":
result = fetch_html(
url=arguments.get('url', ''),
max_chars=arguments.get('max_chars', 800000)
)
else:
result = f"Unbekannte Funktion: {function_name}"
chat_history.append({
"role": "tool",
"tool_call_id": call['id'],
"name": function_name,
"content": result if isinstance(result, str) else "\n".join(result)
})
followup = openai.ChatCompletion.create(
model="qwen/qwen3-4b-2507",
messages=chat_history
)
antwort = followup['choices'][0]['message']['content']
else:
antwort = response_message['content']
return antwort.strip()
except Exception as e:
return f"Fehler: {e}"
def main():
chat_history = [
{"role": "system", "content": "Du bist ein hilfreicher Assistent."}
]
while True:
frage = input("##########\nFrage: ").strip()
if frage.lower() in ['exit', 'quit']:
print("Chat beendet.")
break
chat_history.append({"role": "user", "content": frage})
#antwort = frage_gpt_mit_tools(chat_history)
#print(antwort)
antwort = frage_gpt_mit_tools_stream(chat_history)
chat_history.append({"role": "assistant", "content": antwort})
if __name__ == "__main__":
main()Wie kann ich das so machen das nicht die ganze Ausgabe auf einmal ausgegeben wird sondern Wort für Wort so wie es auch im Chat ist?