lima-city: Webhosting, Domains und Cloud
1 Pluspunkt 0 Minuspunkte

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?

von  

1 Antwort

0 Pluspunkte 0 Minuspunkte

Du musst die "stream=True" Option in der openai.ChatCompletion.create() Methode verwenden und die Antwort inkrementell verarbeiten, während sie vom Server kommt.

def frage_gpt_mit_tools_stream2(chat_history):
    try:
        # Erster Request ohne Streaming → holt Antwort + mögliche Tool-Calls
        response = openai.ChatCompletion.create(
            model="qwen/qwen3-4b-2507",
            messages=chat_history,
            tools=tools,
            tool_choice="auto"
        )

        response_message = response['choices'][0]['message']
        content = response_message.get("content", "")
        tool_calls = response_message.get("tool_calls", [])

        # Normale Antwort ohne Tool-Call
        if not tool_calls:
            return content.strip()

        # Tool-Calls vorhanden
        chat_history.append(response_message)

        for call in tool_calls:
            function_name = call['function']['name']
            arguments = json.loads(call['function']['arguments'])

            print(f"\nTool Call: {function_name} mit Argumenten: {arguments}")
            user_input = input("Erlauben? (y/n): ").strip().lower()
            
            if user_input != "y":
                print("Tool Call abgelehnt.")
                result = f"Tool Call '{function_name}' wurde vom Nutzer abgelehnt."
                chat_history.append({
                    "role": "tool",
                    "tool_call_id": call['id'],
                    "name": function_name,
                    "content": result
                })
                continue

            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)
            })

        # Follow-Up Antwort streamen (optional)
        followup_stream = openai.ChatCompletion.create(
            model="qwen/qwen3-4b-2507",
            messages=chat_history,
            stream=True
        )

        followup_response = ""
        for chunk in followup_stream:
            delta = chunk['choices'][0]['delta']
            if 'content' in delta:
                print(delta['content'], end='', flush=True)
                followup_response += delta['content']

        return followup_response.strip()

    except Exception as e:
        return f"Fehler: {e}"
von (761 Punkte)  
Diese Community basiert auf dem Prinzip der Selbstregulierung. Beiträge werden von Nutzern erstellt, bewertet und verbessert – ganz ohne zentrale Moderation.

Wer hilfreiche Fragen stellt oder gute Antworten gibt, sammelt Punkte. Mit steigender Punktzahl erhalten Mitglieder automatisch mehr Rechte, zum Beispiel

  • Kommentare verfassen
  • Fragen und Antworten bewerten
  • Themen von Fragen bearbeiten
  • Fragen, Antworten und Kommentare bearbeiten
  • Inhalte ausblenden

So entsteht eine Plattform, auf der sich Qualität durchsetzt – getragen von einer engagierten Gemeinschaft.

2,458 Fragen

2,953 Antworten

278 Kommentare

13 Nutzer