1 Pluspunkt 0 Minuspunkte

Ich habe diesen Code um eine Antwort zu streamen.

from flask import Flask, render_template, request, Response
from openai import OpenAI

app = Flask(__name__)

client = OpenAI(
    api_key="", 
    base_url="http://127.0.0.1:8080/v1" 
)

@app.route("/")
def index():
    return render_template("index.html")

@app.route("/chat", methods=["POST"])
def chat():
    system_prompt = request.json.get("system_prompt", "")
    user_message = request.json.get("message", "")

    def generate():
        response = client.chat.completions.create(
            model="deepseek-r1-distill-qwen-7b",
            messages=[{"role": "system", "content": system_prompt}, {"role": "user", "content": user_message}],
            stream=True
        )

        for chunk in response:
            if chunk.choices and chunk.choices[0].delta.content:
                yield chunk.choices[0].delta.content 

    return Response(generate(), content_type="text/plain") 

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000, debug=True, threaded=True)

Wie kann ich aber das Reasoning anzeigen?

von  
Was siehst du in der Browser Console wenn du diesen Teil

for chunk in response:
        if chunk.choices and chunk.choices[0].delta.content:
                yield chunk.choices[0].delta.content

dadurch ersetzt?

for chunk in response:
        yield str(chunk)
ChatCompletionChunk(id='chatcmpl-3836ivfw7epn86fz5wd7h', choices=[Choice(delta=ChoiceDelta(content=None, function_call=None, refusal=None, role='assistant', tool_calls=None, reasoning_content='I'), finish_reason=None, index=0, logprobs=None)], created=1739296661, model='deepseek-r1-distill-qwen-7b', object='chat.completion.chunk', service_tier=None, system_fingerprint='deepseek-r1-distill-qwen-7b', usage=None)
(index):201 ChatCompletionChunk(id='chatcmpl-3836ivfw7epn86fz5wd7h', choices=[Choice(delta=ChoiceDelta(content=None, function_call=None, refusal=None, role='assistant', tool_calls=None, reasoning_content='ch z'), finish_reason=None, index=0, logprobs=None)], created=1739296661, model='deepseek-r1-distill-qwen-7b', object='chat.completion.chunk', service_tier=None, system_fingerprint='deepseek-r1-distill-qwen-7b', usage=None)
(index):201 ChatCompletionChunk(id='chatcmpl-3836ivfw7epn86fz5wd7h', choices=[Choice(delta=ChoiceDelta(content=None, function_call=None, refusal=None, role='assistant', tool_calls=None, reasoning_content='äh'), finish_reason=None, index=0, logprobs=None)], created=1739296661, model='deepseek-r1-distill-qwen-7b', object='chat.completion.chunk', service_tier=None, system_fingerprint='deepseek-r1-distill-qwen-7b', usage=None)
(index):201 ChatCompletionChunk(id='chatcmpl-3836ivfw7epn86fz5wd7h', choices=[Choice(delta=ChoiceDelta(content=None, function_call=None, refusal=None, role='assistant', tool_calls=None, reasoning_content='le '), finish_reason=None, index=0, logprobs=None)], created=1739296661, model='deepseek-r1-distill-qwen-7b', object='chat.completion.chunk', service_tier=None, system_fingerprint='deepseek-r1-distill-qwen-7b', usage=None)
(index):201 ChatCompletionChunk(id='chatcmpl-3836ivfw7epn86fz5wd7h', choices=[Choice(delta=ChoiceDelta(content=None, function_call=None, refusal=None, role='assistant', tool_calls=None, reasoning_content='2 u'), finish_reason=None, index=0, logprobs=None)], created=1739296661, model='deepseek-r1-distill-qwen-7b', object='chat.completion.chunk', service_tier=None, system_fingerprint='deepseek-r1-distill-qwen-7b', usage=None)
(index):201 ChatCompletionChunk(id='chatcmpl-3836ivfw7epn86fz5wd7h', choices=[Choice(delta=ChoiceDelta(content=None, function_call=None, refusal=None, role='assistant', tool_calls=None, reasoning_content='nd 2'), finish_reason=None, index=0, logprobs=None)], created=1739296661, model='deepseek-r1-distill-qwen-7b', object='chat.completion.chunk', service_tier=None, system_fingerprint='deepseek-r1-distill-qwen-7b', usage=None)
(index):201 ChatCompletionChunk(id='chatcmpl-3836ivfw7epn86fz5wd7h', choices=[Choice(delta=ChoiceDelta(content=None, function_call=None, refusal=None, role='assistant', tool_calls=None, reasoning_content=' zu'), finish_reason=None, index=0, logprobs=None)], created=1739296661, model='deepseek-r1-distill-qwen-7b', object='chat.completion.chunk', service_tier=None, system_fingerprint='deepseek-r1-distill-qwen-7b', usage=None)
(index):201 ChatCompletionChunk(id='chatcmpl-3836ivfw7epn86fz5wd7h', choices=[Choice(delta=ChoiceDelta(content=None, function_call=None, refusal=None, role='assistant', tool_calls=None, reasoning_content='sa'), finish_reason=None, index=0, logprobs=None)], created=1739296661, model='deepseek-r1-distill-qwen-7b', object='chat.completion.chunk', service_tier=None, system_fingerprint='deepseek-r1-distill-qwen-7b', usage=None)
(index):201 ChatCompletionChunk(id='chatcmpl-3836ivfw7epn86fz5wd7h', choices=[Choice(delta=ChoiceDelta(content=None, function_call=None, refusal=None, role='assistant', tool_calls=None, reasoning_content='mme'), finish_reason=None, index=0, logprobs=None)], created=1739296661, model='deepseek-r1-distill-qwen-7b', object='chat.completion.chunk', service_tier=None, system_fingerprint='deepseek-r1-distill-qwen-7b', usage=None)
(index):201 ChatCompletionChunk(id='chatcmpl-3836ivfw7epn86fz5wd7h', choices=[Choice(delta=ChoiceDelta(content=None, function_call=None, refusal=None, role='assistant', tool_calls=None, reasoning_content='n.'),
finish_reason=None, index=0, logprobs=None)], created=1739296661, model='deepseek-r1-distill-qwen-7b', object='chat.completion.chunk', service_tier=None, system_fingerprint='deepseek-r1-distill-qwen-7b', usage=None)(index):201 ChatCompletionChunk(id='chatcmpl-3836ivfw7epn86fz5wd7h', choices=[Choice(delta=ChoiceDelta(content='2 + ', function_call=None, refusal=None, role='assistant', tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1739296661, model='deepseek-r1-distill-qwen-7b', object='chat.completion.chunk', service_tier=None, system_fingerprint='deepseek-r1-distill-qwen-7b', usage=None)
(index):201 ChatCompletionChunk(id='chatcmpl-3836ivfw7epn86fz5wd7h', choices=[Choice(delta=ChoiceDelta(content='2 =', function_call=None, refusal=None, role='assistant', tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1739296661, model='deepseek-r1-distill-qwen-7b', object='chat.completion.chunk', service_tier=None, system_fingerprint='deepseek-r1-distill-qwen-7b', usage=None)
(index):201 ChatCompletionChunk(id='chatcmpl-3836ivfw7epn86fz5wd7h', choices=[Choice(delta=ChoiceDelta(content='4.', function_call=None, refusal=None, role='assistant', tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1739296661, model='deepseek-r1-distill-qwen-7b', object='chat.completion.chunk', service_tier=None, system_fingerprint='deepseek-r1-distill-qwen-7b', usage=None)
(index):201 ChatCompletionChunk(id='chatcmpl-3836ivfw7epn86fz5wd7h', choices=[Choice(delta=ChoiceDelta(content=None, function_call=None, refusal=None, role=None, tool_calls=None), finish_reason='stop', index=0, logprobs=None)], created=1739296661, model='deepseek-r1-distill-qwen-7b', object='chat.completion.chunk', service_tier=None, system_fingerprint='deepseek-r1-distill-qwen-7b', usage=None)

1 Antwort

0 Pluspunkte 0 Minuspunkte

In Delta gibt es zwei Felder. Eines für die Antwort (content) und eines für das Thinking (reasoning_content).

for chunk in response:
    if chunk.choices:
        delta = chunk.choices[0].delta
        reasoning_content = getattr(delta, "reasoning_content", None)
        content = getattr(delta, "content", None)
        if reasoning_content: 
            yield reasoning_content
        if content:
            yield content
von (1.0k Punkte)