Buckets:
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"Serve CLI","local":"serve-cli","sections":[{"title":"v1/chat/completions","local":"v1chatcompletions","sections":[{"title":"Hoàn thành dựa trên văn bản","local":"text-based-completions","sections":[],"depth":3},{"title":"Hoàn thành dựa trên hình ảnh","local":"image-based-completions","sections":[],"depth":3},{"title":"Hoàn thành dựa trên âm thanh","local":"audio-based-completions","sections":[],"depth":3},{"title":"Hoàn thành dựa trên video","local":"video-based-completions","sections":[],"depth":3},{"title":"Cuộc trò chuyện nhiều lượt","local":"completions","sections":[],"depth":3}],"depth":2},{"title":"v1/completions","local":"v1completions","sections":[],"depth":2},{"title":"v1/responses","local":"v1responses","sections":[{"title":"Phản hồi dựa trên văn bản","local":"text-based-responses","sections":[],"depth":3},{"title":"Phản hồi dựa trên hình ảnh","local":"image-based-responses","sections":[],"depth":3},{"title":"Phản hồi dựa trên âm thanh","local":"audio-based-responses","sections":[],"depth":3},{"title":"Phản hồi dựa trên video","local":"video-based-responses","sections":[],"depth":3},{"title":"Cuộc trò chuyện nhiều lượt","local":"responses","sections":[],"depth":3}],"depth":2},{"title":"v1/systemone","local":"v1systemone","sections":[{"title":"Cấu hình quyết định","local":"decision-configuration","sections":[],"depth":3}],"depth":2},{"title":"v1/audio/transcriptions","local":"v1audiotranscriptions","sections":[],"depth":2},{"title":"v1/models","local":"v1models","sections":[],"depth":2},{"title":"Tải mô hình","local":"loading-models","sections":[{"title":"Yêu cầu","local":"request","sections":[],"depth":3},{"title":"Phản hồi","local":"response","sections":[],"depth":3},{"title":"Các giai đoạn","local":"stages","sections":[],"depth":3}],"depth":2},{"title":"Timeout","local":"timeout","sections":[{"title":"Đang tải các ví dụ","local":"loading-examples","sections":[],"depth":3}],"depth":2},{"title":"Gọi công cụ","local":"tool-calling","sections":[{"title":"Gọi công cụ đa lượt","local":"multi-turn-tool-calling","sections":[],"depth":3}],"depth":2},{"title":"Lý luận","local":"reasoning","sections":[{"title":"Bật suy luận trên máy chủ","local":"enable-reasoning-on-the-server","sections":[],"depth":3},{"title":"Chat Completions","local":"chat-completions","sections":[],"depth":3},{"title":"Responses API","local":"responses-api","sections":[],"depth":3},{"title":"Đi và về nhiều lượt","local":"multi-turn-round-trip","sections":[],"depth":3}],"depth":2},{"title":"Chuyển tiếp cổng","local":"port-forwarding","sections":[],"depth":2},{"title":"Tính khả tái lập","local":"reproducibility","sections":[],"depth":2}],"depth":1}"/>
<link href="/docs/transformers/main/en/_app/immutable/entry/start.DS27pc2p.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/Bbt6a5UJ.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DGxd7hEC.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/entry/app.JevHRe6k.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/hD794eHD.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DG_DvbXP.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DOLBYqcr.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DsnmJJEf.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/nodes/0.Dperbprz.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DIDdACb8.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/nodes/604.Dwc2d6Ao.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/CBFQlbiL.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/BUd-buby.js" rel="modulepreload">
<!--1rphhhg--><meta name="hf:doc:metadata" content="{"title":"Serve CLI","local":"serve-cli","sections":[{"title":"v1/chat/completions","local":"v1chatcompletions","sections":[{"title":"Text-based completions","local":"text-based-completions","sections":[],"depth":3},{"title":"Image-based completions","local":"image-based-completions","sections":[],"depth":3},{"title":"Audio-based completions","local":"audio-based-completions","sections":[],"depth":3},{"title":"Video-based completions","local":"video-based-completions","sections":[],"depth":3},{"title":"Multi-turn conversations","local":"completions","sections":[],"depth":3}],"depth":2},{"title":"v1/completions","local":"v1completions","sections":[],"depth":2},{"title":"v1/responses","local":"v1responses","sections":[{"title":"Text-based responses","local":"text-based-responses","sections":[],"depth":3},{"title":"Image-based responses","local":"image-based-responses","sections":[],"depth":3},{"title":"Audio-based responses","local":"audio-based-responses","sections":[],"depth":3},{"title":"Video-based responses","local":"video-based-responses","sections":[],"depth":3},{"title":"Multi-turn conversations","local":"responses","sections":[],"depth":3}],"depth":2},{"title":"v1/systemone","local":"v1systemone","sections":[{"title":"Decision configuration","local":"decision-configuration","sections":[],"depth":3}],"depth":2},{"title":"v1/audio/transcriptions","local":"v1audiotranscriptions","sections":[],"depth":2},{"title":"v1/models","local":"v1models","sections":[],"depth":2},{"title":"Loading models","local":"loading-models","sections":[{"title":"Request","local":"request","sections":[],"depth":3},{"title":"Response","local":"response","sections":[],"depth":3},{"title":"Stages","local":"stages","sections":[],"depth":3}],"depth":2},{"title":"Timeout","local":"timeout","sections":[{"title":"Loading examples","local":"loading-examples","sections":[],"depth":3}],"depth":2},{"title":"Tool calling","local":"tool-calling","sections":[{"title":"Multi-turn tool calling","local":"multi-turn-tool-calling","sections":[],"depth":3}],"depth":2},{"title":"Reasoning","local":"reasoning","sections":[{"title":"Enable reasoning on the server","local":"enable-reasoning-on-the-server","sections":[],"depth":3},{"title":"Chat Completions","local":"chat-completions","sections":[],"depth":3},{"title":"Responses API","local":"responses-api","sections":[],"depth":3},{"title":"Multi-turn round trip","local":"multi-turn-round-trip","sections":[],"depth":3}],"depth":2},{"title":"Port forwarding","local":"port-forwarding","sections":[],"depth":2},{"title":"Reproducibility","local":"reproducibility","sections":[],"depth":2}],"depth":1}"/><!---->
<link href="/docs/transformers/main/en/_app/immutable/assets/0.tn0RQdqM.css" rel="modulepreload"> <!--[--><!--[0--><!--[--><!--[0--><!--[--><p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: inline-end; margin-inline-start: 10px; display: inline-flex; position: relative; z-index: 10;"><div dir="ltr" class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----></span> <span dir="auto">Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg><!----></button></div> <!--[-1--><!--]--></div><!----> <!--[0--><h1 class="relative group"><a id="serve-cli" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#serve-cli"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Serve CLI<!----></span></h1><!--]--><!----> <p>CLI <code>transformers serve</code> là một lựa chọn nhẹ nhàng cho các máy chủ cục bộ hoặc tự lưu trữ. Nó tránh được chi phí thời gian chạy và vận hành bổ sung của các bộ máy suy luận chuyên dụng như vLLM. Hãy sử dụng nó cho việc đánh giá, thử nghiệm và triển khai với tải vừa phải. Các tính năng như <a href="../continuous_batching">continuous batching</a> giúp tăng thông lượng và giảm độ trễ.</p> <blockquote class="tip"><p>Đối với triển khai sản xuất quy mô lớn, hãy sử dụng vLLM hoặc SGLang với một mô hình Transformer làm backend. Tìm hiểu thêm trong hướng dẫn <a href="../community_integrations/transformers_as_backend">Các backend suy luận</a>.</p></blockquote> <p>Lệnh <code>transformers serve</code> khởi tạo một máy chủ cục bộ tương thích với <a href="https://platform.openai.com/docs/overview" rel="nofollow">OpenAI SDK</a>. Máy chủ này hoạt động với nhiều ứng dụng bên thứ ba và hỗ trợ các API REST bên dưới.</p> <ul><li><code>/v1/chat/completions</code> cho các yêu cầu văn bản, hình ảnh, âm thanh và video</li> <li><code>/v1/completions</code> cho các hoàn thành văn bản cũ từ một prompt tự do</li> <li><code>/v1/responses</code> hỗ trợ <a href="https://platform.openai.com/docs/api-reference/responses" rel="nofollow">Responses API</a></li> <li><code>/v1/audio/transcriptions</code> cho việc chuyển đổi âm thanh thành văn bản</li> <li><code>/v1/systemone</code> trả lời các câu hỏi có dạng (lựa chọn, điểm số, có/không) với một xác suất cho mỗi câu trả lời được phép</li> <li><code>/v1/models</code> liệt kê các model khả dụng cho các tích hợp bên thứ ba</li> <li><code>/load_model</code> truyền tiến trình tải mô hình qua SSE</li></ul> <p>Cài đặt các phụ thuộc phục vụ.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-bash "><!---->pip install transformers[serving]<!----></pre></div><!----> <p>Chạy <code>transformers serve</code> để khởi động máy chủ. Địa chỉ máy chủ mặc định là http://localhost:8000.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->transformers serve<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="v1chatcompletions" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#v1chatcompletions"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->v1/chat/completions<!----></span></h2><!--]--><!----> <p>API <code>v1/chat/completions</code> được xây dựng dựa trên <a href="https://platform.openai.com/docs/api-reference/chat" rel="nofollow">Chat Completions API</a>. Nó hỗ trợ các yêu cầu văn bản, hình ảnh, âm thanh và video cho LLM, VLM và các mô hình đa phương thức. Hãy sử dụng nó với <code>curl</code>, <a href="https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient" rel="nofollow">InferenceClient</a> hoặc client <a href="https://platform.openai.com/docs/quickstart" rel="nofollow">OpenAI</a>.</p> <!--[2--><h3 class="relative group"><a id="text-based-completions" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#text-based-completions"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Hoàn thành dựa trên văn bản<!----></span></h3><!--]--><!----> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><!--[--><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">huggingface_hub</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">huggingface_hub (dòng)</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">openai</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">openai (dòng)</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">curl</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">curl (dòng)</div><!--]--></div> <div class="language-select"><div><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> InferenceClient
messages = [{<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>, <span class="hljs-string">"content"</span>: <span class="hljs-string">"What is the Transformers library known for?"</span>}]
client = InferenceClient(<span class="hljs-string">"http://localhost:8000"</span>)
result = client.chat_completion(messages, model=<span class="hljs-string">"Qwen/Qwen2.5-0.5B-Instruct"</span>, max_tokens=<span class="hljs-number">256</span>)
<span class="hljs-built_in">print</span>(result.choices[<span class="hljs-number">0</span>].message.content)<!----></pre></div><!----> <p><a href="https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient" rel="nofollow">InferenceClient</a> trả về một chuỗi đã được in ra.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->The Transformers library is primarily known for its ability to create and manipulate large-scale language models [...]<!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> InferenceClient
messages = [{<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>, <span class="hljs-string">"content"</span>: <span class="hljs-string">"What is the Transformers library known for?"</span>}]
client = InferenceClient(<span class="hljs-string">"http://localhost:8000"</span>)
stream = client.chat_completion(messages, model=<span class="hljs-string">"Qwen/Qwen2.5-0.5B-Instruct"</span>, max_tokens=<span class="hljs-number">256</span>, stream=<span class="hljs-literal">True</span>)
<span class="hljs-keyword">for</span> chunk <span class="hljs-keyword">in</span> stream:
token = chunk.choices[<span class="hljs-number">0</span>].delta.content
<span class="hljs-keyword">if</span> token:
<span class="hljs-built_in">print</span>(token, end=<span class="hljs-string">""</span>)<!----></pre></div><!----> <p><a href="https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient" rel="nofollow">InferenceClient</a> trả về một chuỗi đã được in ra.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->The Transformers library is primarily known for its ability to create and manipulate large-scale language models [...]<!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> openai <span class="hljs-keyword">import</span> OpenAI
client = OpenAI(base_url=<span class="hljs-string">"http://localhost:8000/v1"</span>, api_key=<span class="hljs-string">"<random_string>"</span>)
completion = client.chat.completions.create(
model=<span class="hljs-string">"Qwen/Qwen2.5-0.5B-Instruct"</span>,
messages=[
{
<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>,
<span class="hljs-string">"content"</span>: <span class="hljs-string">"What is the Transformers library known for?"</span>
}
],
)
<span class="hljs-built_in">print</span>(completion.choices[<span class="hljs-number">0</span>].message.content)<!----></pre></div><!----> <p>Client <a href="https://platform.openai.com/docs/quickstart" rel="nofollow">OpenAI</a> trả về một chuỗi đã được in ra.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->The Transformers library is primarily known for its ability to create and manipulate large-scale language models [...]<!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> openai <span class="hljs-keyword">import</span> OpenAI
client = OpenAI(base_url=<span class="hljs-string">"http://localhost:8000/v1"</span>, api_key=<span class="hljs-string">"<random_string>"</span>)
completion = client.chat.completions.create(
model=<span class="hljs-string">"Qwen/Qwen2.5-0.5B-Instruct"</span>,
messages=[
{
<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>,
<span class="hljs-string">"content"</span>: <span class="hljs-string">"What is the Transformers library known for?"</span>
}
],
stream=<span class="hljs-literal">True</span>,
)
<span class="hljs-keyword">for</span> chunk <span class="hljs-keyword">in</span> completion:
token = chunk.choices[<span class="hljs-number">0</span>].delta.content
<span class="hljs-keyword">if</span> token:
<span class="hljs-built_in">print</span>(token, end=<span class="hljs-string">""</span>)<!----></pre></div><!----> <p>Client <a href="https://platform.openai.com/docs/quickstart" rel="nofollow">OpenAI</a> trả về một chuỗi đã được in ra.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->The Transformers library is primarily known for its ability to create and manipulate large-scale language models [...]<!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->curl -X POST http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen/Qwen2.5-0.5B-Instruct",
"messages": [{"role": "user", "content": "What is the Transformers library known for?"}],
"temperature": 0.9,
"max_tokens": 256
}'<!----></pre></div><!----> <p>Lệnh trả về một chuỗi JSON.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-json "><!----><span class="hljs-punctuation">{</span>
<span class="hljs-attr">"id"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"ade3578d-e2c3-47ed-b651-c014991a92f6"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"choices"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
<span class="hljs-punctuation">{</span>
<span class="hljs-attr">"finish_reason"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"length"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"index"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"message"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
<span class="hljs-attr">"content"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"The Transformers library is primarily known for its ability to create and manipulate large-scale language models [...]"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"role"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"assistant"</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"created"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1776184004</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"model"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Qwen/Qwen2.5-0.5B-Instruct@main"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"object"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"chat.completion"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"usage"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
<span class="hljs-attr">"completion_tokens"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">256</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"prompt_tokens"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">37</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"total_tokens"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">293</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span><!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->curl -X POST http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen/Qwen2.5-0.5B-Instruct",
"messages": [{"role": "user", "content": "What is the Transformers library known for?"}],
"temperature": 0.9,
"max_tokens": 256,
"stream": true
}'<!----></pre></div><!----> <p>Lệnh trả về một luồng các khối dữ liệu.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->data: {"id":"596d3941-754d-43b4-8773-9bf9940049f5","choices":[{"delta":{"role":"assistant"},"index":0}],"created":1776183469,"model":"Qwen/Qwen2.5-0.5B-Instruct@main","object":"chat.completion.chunk","system_fingerprint":""}
data: {"id":"596d3941-754d-43b4-8773-9bf9940049f5","choices":[{"delta":{"content":"The"},"index":0}],"created":1776183469,"model":"Qwen/Qwen2.5-0.5B-Instruct@main","object":"chat.completion.chunk","system_fingerprint":""}
(...)<!----></pre></div><!----><!----></div><!----><!----></div><!----> <!--[2--><h3 class="relative group"><a id="image-based-completions" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#image-based-completions"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Hoàn thành dựa trên hình ảnh<!----></span></h3><!--]--><!----> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><!--[--><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">huggingface_hub</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">huggingface_hub (dòng)</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">openai</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">openai (dòng)</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">curl</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">curl (dòng)</div><!--]--></div> <div class="language-select"><div><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> InferenceClient
messages = [
{
<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>,
<span class="hljs-string">"content"</span>: [
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"text"</span>, <span class="hljs-string">"text"</span>: <span class="hljs-string">"What's in this image?"</span>},
{
<span class="hljs-string">"type"</span>: <span class="hljs-string">"image_url"</span>,
<span class="hljs-string">"image_url"</span>: {
<span class="hljs-string">"url"</span>: <span class="hljs-string">"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/astronaut.jpg"</span>,
}
},
],
}
]
client = InferenceClient(<span class="hljs-string">"http://localhost:8000"</span>)
result = client.chat_completion(messages, model=<span class="hljs-string">"Qwen/Qwen2.5-VL-7B-Instruct"</span>, max_tokens=<span class="hljs-number">256</span>)
<span class="hljs-built_in">print</span>(result.choices[<span class="hljs-number">0</span>].message.content)<!----></pre></div><!----> <p><a href="https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient" rel="nofollow">InferenceClient</a> trả về một chuỗi đã được in ra.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-xmp "><!---->The image depicts an astronaut <span class="hljs-keyword">in</span> a <span class="hljs-literal">space</span> suit standing <span class="hljs-keyword">on</span> what appears <span class="hljs-keyword">to</span> be <span class="hljs-keyword">the</span> surface <span class="hljs-keyword">of</span> <span class="hljs-keyword">the</span> moon, <span class="hljs-keyword">given</span> <span class="hljs-keyword">the</span> barren, rocky landscape <span class="hljs-keyword">and</span> <span class="hljs-keyword">the</span> dark sky <span class="hljs-keyword">in</span> <span class="hljs-keyword">the</span> background. The astronaut <span class="hljs-keyword">is</span> holding a large egg <span class="hljs-keyword">that</span> has cracked open, revealing a small creature inside. The scene <span class="hljs-keyword">is</span> imaginative <span class="hljs-keyword">and</span> playful, combining elements <span class="hljs-keyword">of</span> <span class="hljs-literal">space</span> exploration <span class="hljs-keyword">with</span> a whimsical twist involving <span class="hljs-keyword">the</span> egg <span class="hljs-keyword">and</span> <span class="hljs-keyword">the</span> creature.<!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> InferenceClient
messages = [
{
<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>,
<span class="hljs-string">"content"</span>: [
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"text"</span>, <span class="hljs-string">"text"</span>: <span class="hljs-string">"What's in this image?"</span>},
{
<span class="hljs-string">"type"</span>: <span class="hljs-string">"image_url"</span>,
<span class="hljs-string">"image_url"</span>: {
<span class="hljs-string">"url"</span>: <span class="hljs-string">"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/astronaut.jpg"</span>,
}
},
],
}
]
client = InferenceClient(<span class="hljs-string">"http://localhost:8000"</span>)
stream = client.chat_completion(messages, model=<span class="hljs-string">"Qwen/Qwen2.5-VL-7B-Instruct"</span>, max_tokens=<span class="hljs-number">256</span>, stream=<span class="hljs-literal">True</span>)
<span class="hljs-keyword">for</span> chunk <span class="hljs-keyword">in</span> stream:
token = chunk.choices[<span class="hljs-number">0</span>].delta.content
<span class="hljs-keyword">if</span> token:
<span class="hljs-built_in">print</span>(token, end=<span class="hljs-string">""</span>)<!----></pre></div><!----> <p><a href="https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient" rel="nofollow">InferenceClient</a> trả về một chuỗi đã được in ra.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-xmp "><!---->The image depicts an astronaut <span class="hljs-keyword">in</span> a <span class="hljs-literal">space</span> suit standing <span class="hljs-keyword">on</span> what appears <span class="hljs-keyword">to</span> be <span class="hljs-keyword">the</span> surface <span class="hljs-keyword">of</span> <span class="hljs-keyword">the</span> moon, <span class="hljs-keyword">given</span> <span class="hljs-keyword">the</span> barren, rocky landscape <span class="hljs-keyword">and</span> <span class="hljs-keyword">the</span> dark sky <span class="hljs-keyword">in</span> <span class="hljs-keyword">the</span> background. The astronaut <span class="hljs-keyword">is</span> holding a large egg <span class="hljs-keyword">that</span> has cracked open, revealing a small creature inside. The scene <span class="hljs-keyword">is</span> imaginative <span class="hljs-keyword">and</span> playful, combining elements <span class="hljs-keyword">of</span> <span class="hljs-literal">space</span> exploration <span class="hljs-keyword">with</span> a whimsical twist involving <span class="hljs-keyword">the</span> egg <span class="hljs-keyword">and</span> <span class="hljs-keyword">the</span> creature.<!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> openai <span class="hljs-keyword">import</span> OpenAI
client = OpenAI(base_url=<span class="hljs-string">"http://localhost:8000/v1"</span>, api_key=<span class="hljs-string">"<random_string>"</span>)
completion = client.chat.completions.create(
model=<span class="hljs-string">"Qwen/Qwen2.5-VL-7B-Instruct"</span>,
messages=[
{
<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>,
<span class="hljs-string">"content"</span>: [
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"text"</span>, <span class="hljs-string">"text"</span>: <span class="hljs-string">"What's in this image?"</span>},
{
<span class="hljs-string">"type"</span>: <span class="hljs-string">"image_url"</span>,
<span class="hljs-string">"image_url"</span>: {
<span class="hljs-string">"url"</span>: <span class="hljs-string">"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/astronaut.jpg"</span>,
}
},
],
}
],
)
<span class="hljs-built_in">print</span>(completion.choices[<span class="hljs-number">0</span>].message.content)<!----></pre></div><!----> <p>Client <a href="https://platform.openai.com/docs/quickstart" rel="nofollow">OpenAI</a> trả về một chuỗi đã được in ra.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-xmp "><!---->The image depicts an astronaut <span class="hljs-keyword">in</span> a <span class="hljs-literal">space</span> suit standing <span class="hljs-keyword">on</span> what appears <span class="hljs-keyword">to</span> be <span class="hljs-keyword">the</span> surface <span class="hljs-keyword">of</span> <span class="hljs-keyword">the</span> moon, <span class="hljs-keyword">given</span> <span class="hljs-keyword">the</span> barren, rocky landscape <span class="hljs-keyword">and</span> <span class="hljs-keyword">the</span> dark sky <span class="hljs-keyword">in</span> <span class="hljs-keyword">the</span> background. The astronaut <span class="hljs-keyword">is</span> holding a large egg <span class="hljs-keyword">that</span> has cracked open, revealing a small creature inside. The scene <span class="hljs-keyword">is</span> imaginative <span class="hljs-keyword">and</span> playful, combining elements <span class="hljs-keyword">of</span> <span class="hljs-literal">space</span> exploration <span class="hljs-keyword">with</span> a whimsical twist involving <span class="hljs-keyword">the</span> egg <span class="hljs-keyword">and</span> <span class="hljs-keyword">the</span> creature.<!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> openai <span class="hljs-keyword">import</span> OpenAI
client = OpenAI(base_url=<span class="hljs-string">"http://localhost:8000/v1"</span>, api_key=<span class="hljs-string">"<random_string>"</span>)
completion = client.chat.completions.create(
model=<span class="hljs-string">"Qwen/Qwen2.5-VL-7B-Instruct"</span>,
messages=[
{
<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>,
<span class="hljs-string">"content"</span>: [
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"text"</span>, <span class="hljs-string">"text"</span>: <span class="hljs-string">"What's in this image?"</span>},
{
<span class="hljs-string">"type"</span>: <span class="hljs-string">"image_url"</span>,
<span class="hljs-string">"image_url"</span>: {
<span class="hljs-string">"url"</span>: <span class="hljs-string">"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/astronaut.jpg"</span>,
}
},
],
}
],
stream=<span class="hljs-literal">True</span>,
)
<span class="hljs-keyword">for</span> chunk <span class="hljs-keyword">in</span> completion:
token = chunk.choices[<span class="hljs-number">0</span>].delta.content
<span class="hljs-keyword">if</span> token:
<span class="hljs-built_in">print</span>(token, end=<span class="hljs-string">""</span>)<!----></pre></div><!----> <p>Client <a href="https://platform.openai.com/docs/quickstart" rel="nofollow">OpenAI</a> trả về một chuỗi đã được in ra.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-xmp "><!---->The image depicts an astronaut <span class="hljs-keyword">in</span> a <span class="hljs-literal">space</span> suit standing <span class="hljs-keyword">on</span> what appears <span class="hljs-keyword">to</span> be <span class="hljs-keyword">the</span> surface <span class="hljs-keyword">of</span> <span class="hljs-keyword">the</span> moon, <span class="hljs-keyword">given</span> <span class="hljs-keyword">the</span> barren, rocky landscape <span class="hljs-keyword">and</span> <span class="hljs-keyword">the</span> dark sky <span class="hljs-keyword">in</span> <span class="hljs-keyword">the</span> background. The astronaut <span class="hljs-keyword">is</span> holding a large egg <span class="hljs-keyword">that</span> has cracked open, revealing a small creature inside. The scene <span class="hljs-keyword">is</span> imaginative <span class="hljs-keyword">and</span> playful, combining elements <span class="hljs-keyword">of</span> <span class="hljs-literal">space</span> exploration <span class="hljs-keyword">with</span> a whimsical twist involving <span class="hljs-keyword">the</span> egg <span class="hljs-keyword">and</span> <span class="hljs-keyword">the</span> creature.<!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->curl http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen/Qwen2.5-VL-7B-Instruct",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What's in this image?"
},
{
"type": "image_url",
"image_url": {
"url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/astronaut.jpg"
}
}
]
}
],
"max_tokens": 256
}'<!----></pre></div><!----> <p>Lệnh trả về một chuỗi JSON.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-json "><!----><span class="hljs-punctuation">{</span>
<span class="hljs-attr">"id"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"f47ac10b-58cc-4372-a567-0e02b2c3d479"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"choices"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
<span class="hljs-punctuation">{</span>
<span class="hljs-attr">"finish_reason"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"stop"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"index"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"message"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
<span class="hljs-attr">"content"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"The image depicts an astronaut in a space suit standing on what appears to be the surface of the moon [...]"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"role"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"assistant"</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"created"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1753366665</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"model"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Qwen/Qwen2.5-VL-7B-Instruct@main"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"object"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"chat.completion"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"usage"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
<span class="hljs-attr">"completion_tokens"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">50</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"prompt_tokens"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">120</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"total_tokens"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">170</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span><!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->curl http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen/Qwen2.5-VL-7B-Instruct",
"stream": true,
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What's in this image?"
},
{
"type": "image_url",
"image_url": {
"url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/astronaut.jpg"
}
}
]
}
],
"max_tokens": 256
}'<!----></pre></div><!----> <p>Lệnh trả về một luồng các khối dữ liệu.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->data: {"id":"f47ac10b-58cc-4372-a567-0e02b2c3d479","choices":[{"delta":{"role":"assistant"},"index":0}],"created":1753366665,"model":"Qwen/Qwen2.5-VL-7B-Instruct@main","object":"chat.completion.chunk","system_fingerprint":""}
data: {"id":"f47ac10b-58cc-4372-a567-0e02b2c3d479","choices":[{"delta":{"content":"The "},"index":0}],"created":1753366701,"model":"Qwen/Qwen2.5-VL-7B-Instruct@main","object":"chat.completion.chunk","system_fingerprint":""}
(...)<!----></pre></div><!----><!----></div><!----><!----></div><!----> <!--[2--><h3 class="relative group"><a id="audio-based-completions" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#audio-based-completions"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Hoàn thành dựa trên âm thanh<!----></span></h3><!--]--><!----> <p>Các mô hình đa phương thức như <a href="https://huggingface.co/google/gemma-4-E2B-it" rel="nofollow">Gemma 4</a> và <a href="https://huggingface.co/Qwen/Qwen2.5-Omni-3B" rel="nofollow">Qwen2.5-Omni</a> nhận đầu vào âm thanh thông qua kiểu nội dung <code>input_audio</code> của OpenAI. Hãy mã hóa Base64 âm thanh và chỉ định định dạng (<code>mp3</code> hoặc <code>wav</code>).</p> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><!--[--><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">huggingface_hub</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">huggingface_hub (dòng)</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">openai</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">openai (dòng)</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">curl</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">curl (dòng)</div><!--]--></div> <div class="language-select"><div><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">import</span> base64
<span class="hljs-keyword">from</span> huggingface_hub.utils <span class="hljs-keyword">import</span> httpx
<span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> InferenceClient
audio_url = <span class="hljs-string">"https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/obama_first_45_secs.mp3"</span>
audio_b64 = base64.b64encode(httpx.get(audio_url, follow_redirects=<span class="hljs-literal">True</span>).content).decode()
messages = [
{
<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>,
<span class="hljs-string">"content"</span>: [
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"text"</span>, <span class="hljs-string">"text"</span>: <span class="hljs-string">"Transcribe this audio."</span>},
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"input_audio"</span>, <span class="hljs-string">"input_audio"</span>: {<span class="hljs-string">"data"</span>: audio_b64, <span class="hljs-string">"format"</span>: <span class="hljs-string">"mp3"</span>}},
],
}
]
client = InferenceClient(<span class="hljs-string">"http://localhost:8000"</span>)
result = client.chat_completion(messages, model=<span class="hljs-string">"google/gemma-4-E2B-it"</span>, max_tokens=<span class="hljs-number">256</span>)
<span class="hljs-built_in">print</span>(result.choices[<span class="hljs-number">0</span>].message.content)<!----></pre></div><!----> <p><a href="https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient" rel="nofollow">InferenceClient</a> trả về một chuỗi đã được in ra.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class=" "><!---->This week, I traveled <span class="hljs-keyword">to</span> Chicago <span class="hljs-keyword">to</span> deliver <span class="hljs-keyword">my</span> final farewell address <span class="hljs-keyword">to</span> <span class="hljs-keyword">the</span> nation, following <span class="hljs-keyword">in</span> <span class="hljs-keyword">the</span> tradition <span class="hljs-keyword">of</span> presidents <span class="hljs-keyword">before</span> <span class="hljs-keyword">me</span>. It was an opportunity <span class="hljs-keyword">to</span> <span class="hljs-built_in">say</span> thank you. Whether we've seen eye <span class="hljs-keyword">to</span> eye <span class="hljs-keyword">or</span> rarely agreed <span class="hljs-keyword">at</span> all, <span class="hljs-keyword">my</span> conversations <span class="hljs-keyword">with</span> you, <span class="hljs-keyword">the</span> American people, <span class="hljs-keyword">in</span> living rooms <span class="hljs-keyword">and</span> schools, <span class="hljs-keyword">at</span> farms <span class="hljs-keyword">and</span> <span class="hljs-keyword">on</span> factory floors, <span class="hljs-keyword">at</span> diners, <span class="hljs-keyword">and</span> <span class="hljs-keyword">on</span> distant military outposts, all these conversations are what have kept <span class="hljs-keyword">me</span> honest.<!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">import</span> base64
<span class="hljs-keyword">from</span> huggingface_hub.utils <span class="hljs-keyword">import</span> httpx
<span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> InferenceClient
audio_url = <span class="hljs-string">"https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/obama_first_45_secs.mp3"</span>
audio_b64 = base64.b64encode(httpx.get(audio_url, follow_redirects=<span class="hljs-literal">True</span>).content).decode()
messages = [
{
<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>,
<span class="hljs-string">"content"</span>: [
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"text"</span>, <span class="hljs-string">"text"</span>: <span class="hljs-string">"Transcribe this audio."</span>},
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"input_audio"</span>, <span class="hljs-string">"input_audio"</span>: {<span class="hljs-string">"data"</span>: audio_b64, <span class="hljs-string">"format"</span>: <span class="hljs-string">"mp3"</span>}},
],
}
]
client = InferenceClient(<span class="hljs-string">"http://localhost:8000"</span>)
stream = client.chat_completion(messages, model=<span class="hljs-string">"google/gemma-4-E2B-it"</span>, max_tokens=<span class="hljs-number">256</span>, stream=<span class="hljs-literal">True</span>)
<span class="hljs-keyword">for</span> chunk <span class="hljs-keyword">in</span> stream:
token = chunk.choices[<span class="hljs-number">0</span>].delta.content
<span class="hljs-keyword">if</span> token:
<span class="hljs-built_in">print</span>(token, end=<span class="hljs-string">""</span>)<!----></pre></div><!----> <p><a href="https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient" rel="nofollow">InferenceClient</a> trả về một chuỗi đã được in ra.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class=" "><!---->This week, I traveled <span class="hljs-keyword">to</span> Chicago <span class="hljs-keyword">to</span> deliver <span class="hljs-keyword">my</span> final farewell address <span class="hljs-keyword">to</span> <span class="hljs-keyword">the</span> nation, following <span class="hljs-keyword">in</span> <span class="hljs-keyword">the</span> tradition <span class="hljs-keyword">of</span> presidents <span class="hljs-keyword">before</span> <span class="hljs-keyword">me</span>. It was an opportunity <span class="hljs-keyword">to</span> <span class="hljs-built_in">say</span> thank you. Whether we've seen eye <span class="hljs-keyword">to</span> eye <span class="hljs-keyword">or</span> rarely agreed <span class="hljs-keyword">at</span> all, <span class="hljs-keyword">my</span> conversations <span class="hljs-keyword">with</span> you, <span class="hljs-keyword">the</span> American people, <span class="hljs-keyword">in</span> living rooms <span class="hljs-keyword">and</span> schools, <span class="hljs-keyword">at</span> farms <span class="hljs-keyword">and</span> <span class="hljs-keyword">on</span> factory floors, <span class="hljs-keyword">at</span> diners, <span class="hljs-keyword">and</span> <span class="hljs-keyword">on</span> distant military outposts, all these conversations are what have kept <span class="hljs-keyword">me</span> honest.<!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">import</span> base64
<span class="hljs-keyword">from</span> huggingface_hub.utils <span class="hljs-keyword">import</span> httpx
<span class="hljs-keyword">from</span> openai <span class="hljs-keyword">import</span> OpenAI
client = OpenAI(base_url=<span class="hljs-string">"http://localhost:8000/v1"</span>, api_key=<span class="hljs-string">"<random_string>"</span>)
audio_url = <span class="hljs-string">"https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/obama_first_45_secs.mp3"</span>
audio_b64 = base64.b64encode(httpx.get(audio_url, follow_redirects=<span class="hljs-literal">True</span>).content).decode()
completion = client.chat.completions.create(
model=<span class="hljs-string">"google/gemma-4-E2B-it"</span>,
messages=[
{
<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>,
<span class="hljs-string">"content"</span>: [
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"text"</span>, <span class="hljs-string">"text"</span>: <span class="hljs-string">"Transcribe this audio."</span>},
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"input_audio"</span>, <span class="hljs-string">"input_audio"</span>: {<span class="hljs-string">"data"</span>: audio_b64, <span class="hljs-string">"format"</span>: <span class="hljs-string">"mp3"</span>}},
],
}
],
)
<span class="hljs-built_in">print</span>(completion.choices[<span class="hljs-number">0</span>].message.content)<!----></pre></div><!----> <p>Client <a href="https://platform.openai.com/docs/quickstart" rel="nofollow">OpenAI</a> trả về một chuỗi đã được in ra.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class=" "><!---->This week, I traveled <span class="hljs-keyword">to</span> Chicago <span class="hljs-keyword">to</span> deliver <span class="hljs-keyword">my</span> final farewell address <span class="hljs-keyword">to</span> <span class="hljs-keyword">the</span> nation, following <span class="hljs-keyword">in</span> <span class="hljs-keyword">the</span> tradition <span class="hljs-keyword">of</span> presidents <span class="hljs-keyword">before</span> <span class="hljs-keyword">me</span>. It was an opportunity <span class="hljs-keyword">to</span> <span class="hljs-built_in">say</span> thank you. Whether we've seen eye <span class="hljs-keyword">to</span> eye <span class="hljs-keyword">or</span> rarely agreed <span class="hljs-keyword">at</span> all, <span class="hljs-keyword">my</span> conversations <span class="hljs-keyword">with</span> you, <span class="hljs-keyword">the</span> American people, <span class="hljs-keyword">in</span> living rooms <span class="hljs-keyword">and</span> schools, <span class="hljs-keyword">at</span> farms <span class="hljs-keyword">and</span> <span class="hljs-keyword">on</span> factory floors, <span class="hljs-keyword">at</span> diners, <span class="hljs-keyword">and</span> <span class="hljs-keyword">on</span> distant military outposts, all these conversations are what have kept <span class="hljs-keyword">me</span> honest.<!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">import</span> base64
<span class="hljs-keyword">from</span> huggingface_hub.utils <span class="hljs-keyword">import</span> httpx
<span class="hljs-keyword">from</span> openai <span class="hljs-keyword">import</span> OpenAI
client = OpenAI(base_url=<span class="hljs-string">"http://localhost:8000/v1"</span>, api_key=<span class="hljs-string">"<random_string>"</span>)
audio_url = <span class="hljs-string">"https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/obama_first_45_secs.mp3"</span>
audio_b64 = base64.b64encode(httpx.get(audio_url, follow_redirects=<span class="hljs-literal">True</span>).content).decode()
completion = client.chat.completions.create(
model=<span class="hljs-string">"google/gemma-4-E2B-it"</span>,
messages=[
{
<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>,
<span class="hljs-string">"content"</span>: [
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"text"</span>, <span class="hljs-string">"text"</span>: <span class="hljs-string">"Transcribe this audio."</span>},
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"input_audio"</span>, <span class="hljs-string">"input_audio"</span>: {<span class="hljs-string">"data"</span>: audio_b64, <span class="hljs-string">"format"</span>: <span class="hljs-string">"mp3"</span>}},
],
}
],
stream=<span class="hljs-literal">True</span>,
)
<span class="hljs-keyword">for</span> chunk <span class="hljs-keyword">in</span> completion:
token = chunk.choices[<span class="hljs-number">0</span>].delta.content
<span class="hljs-keyword">if</span> token:
<span class="hljs-built_in">print</span>(token, end=<span class="hljs-string">""</span>)<!----></pre></div><!----> <p>Client <a href="https://platform.openai.com/docs/quickstart" rel="nofollow">OpenAI</a> trả về một chuỗi đã được in ra.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class=" "><!---->This week, I traveled <span class="hljs-keyword">to</span> Chicago <span class="hljs-keyword">to</span> deliver <span class="hljs-keyword">my</span> final farewell address <span class="hljs-keyword">to</span> <span class="hljs-keyword">the</span> nation, following <span class="hljs-keyword">in</span> <span class="hljs-keyword">the</span> tradition <span class="hljs-keyword">of</span> presidents <span class="hljs-keyword">before</span> <span class="hljs-keyword">me</span>. It was an opportunity <span class="hljs-keyword">to</span> <span class="hljs-built_in">say</span> thank you. Whether we've seen eye <span class="hljs-keyword">to</span> eye <span class="hljs-keyword">or</span> rarely agreed <span class="hljs-keyword">at</span> all, <span class="hljs-keyword">my</span> conversations <span class="hljs-keyword">with</span> you, <span class="hljs-keyword">the</span> American people, <span class="hljs-keyword">in</span> living rooms <span class="hljs-keyword">and</span> schools, <span class="hljs-keyword">at</span> farms <span class="hljs-keyword">and</span> <span class="hljs-keyword">on</span> factory floors, <span class="hljs-keyword">at</span> diners, <span class="hljs-keyword">and</span> <span class="hljs-keyword">on</span> distant military outposts, all these conversations are what have kept <span class="hljs-keyword">me</span> honest.<!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!----><span class="hljs-meta prompt_"># </span><span class="language-bash">First, base64-encode the audio file and build the JSON payload</span>
AUDIO_B64=$(curl -sL https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/obama_first_45_secs.mp3 | base64 -w 0)
cat <<EOF > /tmp/audio_request.json
{
"model": "google/gemma-4-E2B-it",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "Transcribe this audio."},
{"type": "input_audio", "input_audio": {"data": "$AUDIO_B64", "format": "mp3"}}
]
}
]
}
EOF
curl http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d @/tmp/audio_request.json<!----></pre></div><!----> <p>Lệnh trả về một chuỗi JSON.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-json "><!----><span class="hljs-punctuation">{</span>
<span class="hljs-attr">"id"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"a1b2c3d4-e5f6-7890-abcd-ef1234567890"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"choices"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
<span class="hljs-punctuation">{</span>
<span class="hljs-attr">"finish_reason"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"stop"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"index"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"message"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
<span class="hljs-attr">"content"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"This week, I traveled to Chicago to deliver my final farewell address to the nation [...]"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"role"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"assistant"</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"created"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1753366665</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"model"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"google/gemma-4-E2B-it@main"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"object"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"chat.completion"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"usage"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
<span class="hljs-attr">"completion_tokens"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">85</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"prompt_tokens"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">200</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"total_tokens"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">285</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span><!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!----><span class="hljs-meta prompt_"># </span><span class="language-bash">First, base64-encode the audio file and build the JSON payload</span>
AUDIO_B64=$(curl -sL https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/obama_first_45_secs.mp3 | base64 -w 0)
cat <<EOF > /tmp/audio_request.json
{
"model": "google/gemma-4-E2B-it",
"stream": true,
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "Transcribe this audio."},
{"type": "input_audio", "input_audio": {"data": "$AUDIO_B64", "format": "mp3"}}
]
}
]
}
EOF
curl http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d @/tmp/audio_request.json<!----></pre></div><!----> <p>Lệnh trả về một luồng các khối dữ liệu.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->data: {"id":"cb997e1d-98b9-414a-be89-1880288610ef","choices":[{"delta":{"role":"assistant"},"index":0}],"created":1776254856,"model":"google/gemma-4-E2B-it@main","object":"chat.completion.chunk","system_fingerprint":""}
data: {"id":"cb997e1d-98b9-414a-be89-1880288610ef","choices":[{"delta":{"content":"This"},"index":0}],"created":1776254856,"model":"google/gemma-4-E2B-it@main","object":"chat.completion.chunk","system_fingerprint":""}
(...)<!----></pre></div><!----><!----></div><!----><!----></div><!----> <blockquote class="warning"><p>Loại nội dung <code>audio_url</code> là một phần mở rộng không thuộc chuẩn OpenAI và có thể thay đổi trong các phiên bản tương lai.</p></blockquote> <p>Bạn cũng có thể truyền audio bằng URL với kiểu nội dung <code>audio_url</code> để bỏ qua mã hóa base64.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!---->completion = client.chat.completions.create(
model=<span class="hljs-string">"google/gemma-4-E2B-it"</span>,
messages=[
{
<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>,
<span class="hljs-string">"content"</span>: [
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"text"</span>, <span class="hljs-string">"text"</span>: <span class="hljs-string">"Transcribe this audio."</span>},
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"audio_url"</span>, <span class="hljs-string">"audio_url"</span>: {<span class="hljs-string">"url"</span>: <span class="hljs-string">"https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/obama_first_45_secs.mp3"</span>}},
],
}
],
)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="video-based-completions" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#video-based-completions"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Hoàn thành dựa trên video<!----></span></h3><!--]--><!----> <blockquote class="warning"><p>Loại nội dung <code>video_url</code> là một phần mở rộng không thuộc chuẩn OpenAI và có thể thay đổi trong các phiên bản tương lai.</p></blockquote> <p>Sử dụng kiểu nội dung <code>video_url</code> cho đầu vào video. Nếu mô hình hỗ trợ âm thanh (ví dụ: Gemma 4, Qwen2.5-Omni), máy chủ sẽ trích xuất bản âm thanh từ video và xử lý nó cùng với các khung hình trực quan.</p> <blockquote class="tip"><p>Xử lý video yêu cầu <a href="https://github.com/pytorch/torchcodec" rel="nofollow">torchcodec</a>. Cài đặt bằng <code>pip install torchcodec</code>.</p></blockquote> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><!--[--><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">huggingface_hub</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">huggingface_hub (dòng)</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">openai</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">openai (dòng)</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">curl</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">curl (dòng)</div><!--]--></div> <div class="language-select"><div><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> InferenceClient
messages = [
{
<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>,
<span class="hljs-string">"content"</span>: [
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"video_url"</span>, <span class="hljs-string">"video_url"</span>: {<span class="hljs-string">"url"</span>: <span class="hljs-string">"https://huggingface.co/datasets/merve/vlm_test_images/resolve/main/concert.mp4"</span>}},
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"text"</span>, <span class="hljs-string">"text"</span>: <span class="hljs-string">"What is happening in the video and what is the song about?"</span>},
],
}
]
client = InferenceClient(<span class="hljs-string">"http://localhost:8000"</span>)
result = client.chat_completion(messages, model=<span class="hljs-string">"google/gemma-4-E2B-it"</span>, max_tokens=<span class="hljs-number">256</span>)
<span class="hljs-built_in">print</span>(result.choices[<span class="hljs-number">0</span>].message.content)<!----></pre></div><!----> <p><a href="https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient" rel="nofollow">InferenceClient</a> trả về một chuỗi đã được in ra.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class=" "><!---->The video captures a live music performance at a music festival<span class="hljs-built_in"> or </span>a large concert. There are several musicians on stage, including a central figure playing an acoustic guitar<span class="hljs-built_in"> and </span>singing. The foreground is<span class="hljs-built_in"> filled </span>with the backs of the audience, indicating a large crowd watching the show. The stage is dramatically lit with bright spotlights<span class="hljs-built_in"> and </span>blue<span class="hljs-built_in"> and </span>white stage lighting, with haze<span class="hljs-built_in"> and </span>smoke creating an immersive atmosphere.
The lyrics of the song are: <span class="hljs-string">"I don't care 'bout street, from that fresh street, 'cause there's no problem, another one I want to be, in the storm..."</span><!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> InferenceClient
messages = [
{
<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>,
<span class="hljs-string">"content"</span>: [
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"video_url"</span>, <span class="hljs-string">"video_url"</span>: {<span class="hljs-string">"url"</span>: <span class="hljs-string">"https://huggingface.co/datasets/merve/vlm_test_images/resolve/main/concert.mp4"</span>}},
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"text"</span>, <span class="hljs-string">"text"</span>: <span class="hljs-string">"What is happening in the video and what is the song about?"</span>},
],
}
]
client = InferenceClient(<span class="hljs-string">"http://localhost:8000"</span>)
stream = client.chat_completion(messages, model=<span class="hljs-string">"google/gemma-4-E2B-it"</span>, max_tokens=<span class="hljs-number">256</span>, stream=<span class="hljs-literal">True</span>)
<span class="hljs-keyword">for</span> chunk <span class="hljs-keyword">in</span> stream:
token = chunk.choices[<span class="hljs-number">0</span>].delta.content
<span class="hljs-keyword">if</span> token:
<span class="hljs-built_in">print</span>(token, end=<span class="hljs-string">""</span>)<!----></pre></div><!----> <p><a href="https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient" rel="nofollow">InferenceClient</a> trả về một chuỗi đã được in ra.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class=" "><!---->The video captures a live music performance at a music festival<span class="hljs-built_in"> or </span>a large concert. There are several musicians on stage, including a central figure playing an acoustic guitar<span class="hljs-built_in"> and </span>singing. The foreground is<span class="hljs-built_in"> filled </span>with the backs of the audience, indicating a large crowd watching the show. The stage is dramatically lit with bright spotlights<span class="hljs-built_in"> and </span>blue<span class="hljs-built_in"> and </span>white stage lighting, with haze<span class="hljs-built_in"> and </span>smoke creating an immersive atmosphere.
The lyrics of the song are: <span class="hljs-string">"I don't care 'bout street, from that fresh street, 'cause there's no problem, another one I want to be, in the storm..."</span><!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> openai <span class="hljs-keyword">import</span> OpenAI
client = OpenAI(base_url=<span class="hljs-string">"http://localhost:8000/v1"</span>, api_key=<span class="hljs-string">"<random_string>"</span>)
completion = client.chat.completions.create(
model=<span class="hljs-string">"google/gemma-4-E2B-it"</span>,
messages=[
{
<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>,
<span class="hljs-string">"content"</span>: [
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"video_url"</span>, <span class="hljs-string">"video_url"</span>: {<span class="hljs-string">"url"</span>: <span class="hljs-string">"https://huggingface.co/datasets/merve/vlm_test_images/resolve/main/concert.mp4"</span>}},
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"text"</span>, <span class="hljs-string">"text"</span>: <span class="hljs-string">"What is happening in the video and what is the song about?"</span>},
],
}
],
max_tokens=<span class="hljs-number">256</span>,
)
<span class="hljs-built_in">print</span>(completion.choices[<span class="hljs-number">0</span>].message.content)<!----></pre></div><!----> <p>Client <a href="https://platform.openai.com/docs/quickstart" rel="nofollow">OpenAI</a> trả về một chuỗi đã được in ra.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class=" "><!---->The video captures a live music performance at a music festival<span class="hljs-built_in"> or </span>a large concert. There are several musicians on stage, including a central figure playing an acoustic guitar<span class="hljs-built_in"> and </span>singing. The foreground is<span class="hljs-built_in"> filled </span>with the backs of the audience, indicating a large crowd watching the show. The stage is dramatically lit with bright spotlights<span class="hljs-built_in"> and </span>blue<span class="hljs-built_in"> and </span>white stage lighting, with haze<span class="hljs-built_in"> and </span>smoke creating an immersive atmosphere.
The lyrics of the song are: <span class="hljs-string">"I don't care 'bout street, from that fresh street, 'cause there's no problem, another one I want to be, in the storm..."</span><!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> openai <span class="hljs-keyword">import</span> OpenAI
client = OpenAI(base_url=<span class="hljs-string">"http://localhost:8000/v1"</span>, api_key=<span class="hljs-string">"<random_string>"</span>)
completion = client.chat.completions.create(
model=<span class="hljs-string">"google/gemma-4-E2B-it"</span>,
messages=[
{
<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>,
<span class="hljs-string">"content"</span>: [
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"video_url"</span>, <span class="hljs-string">"video_url"</span>: {<span class="hljs-string">"url"</span>: <span class="hljs-string">"https://huggingface.co/datasets/merve/vlm_test_images/resolve/main/concert.mp4"</span>}},
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"text"</span>, <span class="hljs-string">"text"</span>: <span class="hljs-string">"What is happening in the video and what is the song about?"</span>},
],
}
],
max_tokens=<span class="hljs-number">256</span>,
stream=<span class="hljs-literal">True</span>,
)
<span class="hljs-keyword">for</span> chunk <span class="hljs-keyword">in</span> completion:
token = chunk.choices[<span class="hljs-number">0</span>].delta.content
<span class="hljs-keyword">if</span> token:
<span class="hljs-built_in">print</span>(token, end=<span class="hljs-string">""</span>)<!----></pre></div><!----> <p>Client <a href="https://platform.openai.com/docs/quickstart" rel="nofollow">OpenAI</a> trả về một chuỗi đã được in ra.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class=" "><!---->The video captures a live music performance at a music festival<span class="hljs-built_in"> or </span>a large concert. There are several musicians on stage, including a central figure playing an acoustic guitar<span class="hljs-built_in"> and </span>singing. The foreground is<span class="hljs-built_in"> filled </span>with the backs of the audience, indicating a large crowd watching the show. The stage is dramatically lit with bright spotlights<span class="hljs-built_in"> and </span>blue<span class="hljs-built_in"> and </span>white stage lighting, with haze<span class="hljs-built_in"> and </span>smoke creating an immersive atmosphere.
The lyrics of the song are: <span class="hljs-string">"I don't care 'bout street, from that fresh street, 'cause there's no problem, another one I want to be, in the storm..."</span><!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->curl http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "google/gemma-4-E2B-it",
"messages": [
{
"role": "user",
"content": [
{"type": "video_url", "video_url": {"url": "https://huggingface.co/datasets/merve/vlm_test_images/resolve/main/concert.mp4"}},
{"type": "text", "text": "What is happening in the video and what is the song about?"}
]
}
],
"max_tokens": 256
}'<!----></pre></div><!----> <p>Lệnh trả về một chuỗi JSON.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-json "><!----><span class="hljs-punctuation">{</span>
<span class="hljs-attr">"id"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"b2c3d4e5-f6a7-8901-bcde-f12345678901"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"choices"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
<span class="hljs-punctuation">{</span>
<span class="hljs-attr">"finish_reason"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"length"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"index"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"message"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
<span class="hljs-attr">"content"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"The video captures a live music performance at a music festival or a large concert [...]"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"role"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"assistant"</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"created"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1753366665</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"model"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"google/gemma-4-E2B-it@main"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"object"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"chat.completion"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"usage"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
<span class="hljs-attr">"completion_tokens"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">256</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"prompt_tokens"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">350</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"total_tokens"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">606</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span><!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->curl http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "google/gemma-4-E2B-it",
"stream": true,
"messages": [
{
"role": "user",
"content": [
{"type": "video_url", "video_url": {"url": "https://huggingface.co/datasets/merve/vlm_test_images/resolve/main/concert.mp4"}},
{"type": "text", "text": "What is happening in the video and what is the song about?"}
]
}
],
"max_tokens": 256
}'<!----></pre></div><!----> <p>Lệnh trả về một luồng các khối dữ liệu.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->data: {"id":"cb997e1d-98b9-414a-be89-1880288610ef","choices":[{"delta":{"role":"assistant"},"index":0}],"created":1776254856,"model":"google/gemma-4-E2B-it@main","object":"chat.completion.chunk","system_fingerprint":""}
data: {"id":"cb997e1d-98b9-414a-be89-1880288610ef","choices":[{"delta":{"content":"Based"},"index":0}],"created":1776254856,"model":"google/gemma-4-E2B-it@main","object":"chat.completion.chunk","system_fingerprint":""}
(...)<!----></pre></div><!----><!----></div><!----><!----></div><!----> <!--[2--><h3 class="relative group"><a id="completions" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#completions"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Cuộc trò chuyện nhiều lượt<!----></span></h3><!--]--><!----> <p>Để có một cuộc trò chuyện nhiều lượt, hãy bao gồm lịch sử trò chuyện đầy đủ trong danh sách <code>messages</code> với các vai trò <code>user</code> và <code>assistant</code> xen kẽ. Giống như tất cả các máy chủ tương thích OpenAI, API không có trạng thái, vì vậy mỗi yêu cầu phải chứa lịch sử trò chuyện hoàn chỉnh.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> openai <span class="hljs-keyword">import</span> OpenAI
client = OpenAI(base_url=<span class="hljs-string">"http://localhost:8000/v1"</span>, api_key=<span class="hljs-string">"<random_string>"</span>)
completion = client.chat.completions.create(
model=<span class="hljs-string">"Qwen/Qwen2.5-0.5B-Instruct"</span>,
messages=[
{<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>, <span class="hljs-string">"content"</span>: <span class="hljs-string">"What is the capital of France?"</span>},
{<span class="hljs-string">"role"</span>: <span class="hljs-string">"assistant"</span>, <span class="hljs-string">"content"</span>: <span class="hljs-string">"The capital of France is Paris."</span>},
{<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>, <span class="hljs-string">"content"</span>: <span class="hljs-string">"How many people live there?"</span>},
],
)
<span class="hljs-built_in">print</span>(completion.choices[<span class="hljs-number">0</span>].message.content)<!----></pre></div><!----> <p>Câu hỏi tiếp theo “Có bao nhiêu người sống ở đó?” dựa vào ngữ cảnh trước đó, vì vậy mô hình trả lời về Paris.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class=" "><!----><span class="hljs-attribute">As</span> of <span class="hljs-number">2021</span>, the population of Paris is approximately <span class="hljs-number">2</span>.<span class="hljs-number">2</span> million people.<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="v1completions" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#v1completions"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->v1/completions<!----></span></h2><!--]--><!----> <p>API <code>v1/completions</code> dựa trên <a href="https://platform.openai.com/docs/api-reference/completions" rel="nofollow">API Completions cũ</a>. Khác với <code>/v1/chat/completions</code>, API này nhận một <code>prompt</code> dạng văn bản tự do thay vì các tin nhắn chat và trả về văn bản được tạo trong <code>choices[].text</code>. Điều này hữu ích cho các mô hình cơ bản (không phải instruct) và các tác vụ hoàn thành văn bản mà không cần mẫu chat. Nó cũng hỗ trợ <code>suffix</code> để chèn văn bản ở giữa.</p> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><!--[--><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">curl</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">openai</div><!--]--></div> <div class="language-select"><div><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->curl -X POST http://localhost:8000/v1/completions \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen/Qwen2.5-0.5B",
"prompt": "The capital of France is",
"max_tokens": 20
}'<!----></pre></div><!----> <p>Lệnh trả về phản hồi sau.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-json "><!----><span class="hljs-punctuation">{</span>
<span class="hljs-attr">"id"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"chatcmpl-abc123"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"object"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"text_completion"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"created"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1234567890</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"model"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Qwen/Qwen2.5-0.5B@main"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"choices"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
<span class="hljs-punctuation">{</span>
<span class="hljs-attr">"text"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">" Paris, and the capital of the United States is Washington, D.C."</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"index"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"logprobs"</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">null</span></span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"finish_reason"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"stop"</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"usage"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
<span class="hljs-attr">"prompt_tokens"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">5</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"completion_tokens"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">16</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"total_tokens"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">21</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span><!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> openai <span class="hljs-keyword">import</span> OpenAI
client = OpenAI(base_url=<span class="hljs-string">"http://localhost:8000/v1"</span>, api_key=<span class="hljs-string">"<random_string>"</span>)
<span class="hljs-comment"># Non-streaming</span>
completion = client.completions.create(
model=<span class="hljs-string">"Qwen/Qwen2.5-0.5B"</span>,
prompt=<span class="hljs-string">"The capital of France is"</span>,
max_tokens=<span class="hljs-number">20</span>,
)
<span class="hljs-built_in">print</span>(completion.choices[<span class="hljs-number">0</span>].text)<!----></pre></div><!----> <p>Client <a href="https://platform.openai.com/docs/quickstart" rel="nofollow">OpenAI</a> trả về các nội dung sau.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!----> Paris, and the capital of the United States is Washington, D.C.<!----></pre></div><!----> <p>Streaming cũng được hỗ trợ.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!---->stream = client.completions.create(
model=<span class="hljs-string">"Qwen/Qwen2.5-0.5B"</span>,
prompt=<span class="hljs-string">"The capital of France is"</span>,
max_tokens=<span class="hljs-number">20</span>,
stream=<span class="hljs-literal">True</span>,
)
<span class="hljs-keyword">for</span> chunk <span class="hljs-keyword">in</span> stream:
<span class="hljs-built_in">print</span>(chunk.choices[<span class="hljs-number">0</span>].text, end=<span class="hljs-string">""</span>)<!----></pre></div><!----><!----></div><!----><!----></div><!----> <!--[1--><h2 class="relative group"><a id="v1responses" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#v1responses"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->v1/responses<!----></span></h2><!--]--><!----> <p><a href="https://platform.openai.com/docs/api-reference/responses" rel="nofollow">Responses API</a> là điểm cuối API mới nhất của OpenAI cho việc tạo nội dung. Nó hỗ trợ các tương tác có trạng thái và tích hợp các công cụ tích hợp sẵn để mở rộng khả năng của mô hình. OpenAI <a href="https://platform.openai.com/docs/guides/migrate-to-responses" rel="nofollow">khuyến nghị</a> sử dụng Responses API thay vì Chat Completions API cho các dự án mới.</p> <p>API <code>v1/responses</code> hỗ trợ các yêu cầu văn bản, hình ảnh, âm thanh và video thông qua lệnh <code>curl</code> và client <a href="https://platform.openai.com/docs/quickstart" rel="nofollow">OpenAI</a>.</p> <!--[2--><h3 class="relative group"><a id="text-based-responses" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#text-based-responses"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Phản hồi dựa trên văn bản<!----></span></h3><!--]--><!----> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><!--[--><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">openai</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">openai (dòng)</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">curl</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">curl (dòng)</div><!--]--></div> <div class="language-select"><div><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> openai <span class="hljs-keyword">import</span> OpenAI
client = OpenAI(base_url=<span class="hljs-string">"http://localhost:8000/v1"</span>, api_key=<span class="hljs-string">"<random_string>"</span>)
response = client.responses.create(
model=<span class="hljs-string">"Qwen/Qwen2.5-0.5B-Instruct"</span>,
<span class="hljs-built_in">input</span>=<span class="hljs-string">"Tell me a three sentence bedtime story about a unicorn."</span>,
max_output_tokens=<span class="hljs-number">256</span>,
stream=<span class="hljs-literal">False</span>,
)
<span class="hljs-built_in">print</span>(response.output[<span class="hljs-number">0</span>].content[<span class="hljs-number">0</span>].text)<!----></pre></div><!----> <p>Client <a href="https://platform.openai.com/docs/quickstart" rel="nofollow">OpenAI</a> trả về một chuỗi đã được in ra.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->Once upon a time, in a faraway land, there lived a beautiful unicorn named Luna [...]<!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> openai <span class="hljs-keyword">import</span> OpenAI
client = OpenAI(base_url=<span class="hljs-string">"http://localhost:8000/v1"</span>, api_key=<span class="hljs-string">"<random_string>"</span>)
response = client.responses.create(
model=<span class="hljs-string">"Qwen/Qwen2.5-0.5B-Instruct"</span>,
<span class="hljs-built_in">input</span>=<span class="hljs-string">"Tell me a three sentence bedtime story about a unicorn."</span>,
max_output_tokens=<span class="hljs-number">256</span>,
stream=<span class="hljs-literal">True</span>,
)
<span class="hljs-keyword">for</span> event <span class="hljs-keyword">in</span> response:
<span class="hljs-keyword">if</span> <span class="hljs-built_in">hasattr</span>(event, <span class="hljs-string">"delta"</span>) <span class="hljs-keyword">and</span> event.delta:
<span class="hljs-built_in">print</span>(event.delta, end=<span class="hljs-string">""</span>)<!----></pre></div><!----> <p>Client <a href="https://platform.openai.com/docs/quickstart" rel="nofollow">OpenAI</a> trả về một chuỗi đã được in ra.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->Once upon a time, in a faraway land, there lived a beautiful unicorn named Luna [...]<!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->curl http://localhost:8000/v1/responses \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen/Qwen2.5-0.5B-Instruct",
"input": "Tell me a three sentence bedtime story about a unicorn.",
"max_output_tokens": 256,
"stream": false
}'<!----></pre></div><!----> <p>Lệnh trả về một chuỗi JSON.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-json "><!----><span class="hljs-punctuation">{</span>
<span class="hljs-attr">"id"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"resp_8693af9b-b561-4267-bd1a-22d1b43284cb"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"created_at"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1776258580.7114341</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"model"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Qwen/Qwen2.5-0.5B-Instruct@main"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"object"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"response"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"output"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
<span class="hljs-punctuation">{</span>
<span class="hljs-attr">"id"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"msg_8693af9b-b561-4267-bd1a-22d1b43284cb"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"content"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
<span class="hljs-punctuation">{</span>
<span class="hljs-attr">"annotations"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"text"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Once upon a time, in a faraway land, there lived a beautiful unicorn named Luna [...]"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"output_text"</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"role"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"assistant"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"status"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"completed"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"message"</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"status"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"completed"</span>
<span class="hljs-punctuation">}</span><!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->curl http://localhost:8000/v1/responses \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen/Qwen2.5-0.5B-Instruct",
"stream": true,
"input": "Tell me a three sentence bedtime story about a unicorn.",
"max_output_tokens": 256
}'<!----></pre></div><!----> <p>Lệnh trả về một luồng các khối dữ liệu.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->data: {"response":{"id":"resp_req_0","created_at":1754059817.783648,"model":"Qwen/Qwen2.5-0.5B-Instruct@main","object":"response","output":[],"status":"queued"},"sequence_number":0,"type":"response.created"}
data: {"content_index":0,"delta":"Once ","item_id":"msg_req_0","output_index":0,"sequence_number":5,"type":"response.output_text.delta"}
data: {"content_index":0,"delta":"upon ","item_id":"msg_req_0","output_index":0,"sequence_number":6,"type":"response.output_text.delta"}
(...)<!----></pre></div><!----><!----></div><!----><!----></div><!----> <!--[2--><h3 class="relative group"><a id="image-based-responses" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#image-based-responses"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Phản hồi dựa trên hình ảnh<!----></span></h3><!--]--><!----> <p>API Responses cũng hỗ trợ đầu vào hình ảnh, âm thanh và video.</p> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><!--[--><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">openai</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">openai (dòng)</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">curl</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">curl (dòng)</div><!--]--></div> <div class="language-select"><div><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> openai <span class="hljs-keyword">import</span> OpenAI
client = OpenAI(base_url=<span class="hljs-string">"http://localhost:8000/v1"</span>, api_key=<span class="hljs-string">"<random_string>"</span>)
response = client.responses.create(
model=<span class="hljs-string">"Qwen/Qwen2.5-VL-7B-Instruct"</span>,
<span class="hljs-built_in">input</span>=[
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"input_text"</span>, <span class="hljs-string">"text"</span>: <span class="hljs-string">"What's in this image?"</span>},
{
<span class="hljs-string">"type"</span>: <span class="hljs-string">"input_image"</span>,
<span class="hljs-string">"image_url"</span>: <span class="hljs-string">"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/astronaut.jpg"</span>,
},
],
max_output_tokens=<span class="hljs-number">256</span>,
stream=<span class="hljs-literal">False</span>,
)
<span class="hljs-built_in">print</span>(response.output[<span class="hljs-number">0</span>].content[<span class="hljs-number">0</span>].text)<!----></pre></div><!----> <p>Client <a href="https://platform.openai.com/docs/quickstart" rel="nofollow">OpenAI</a> trả về một chuỗi đã được in ra.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-xmp "><!---->The image depicts an astronaut <span class="hljs-keyword">in</span> a <span class="hljs-literal">space</span> suit standing <span class="hljs-keyword">on</span> what appears <span class="hljs-keyword">to</span> be <span class="hljs-keyword">the</span> surface <span class="hljs-keyword">of</span> <span class="hljs-keyword">the</span> moon, <span class="hljs-keyword">given</span> <span class="hljs-keyword">the</span> barren, rocky landscape <span class="hljs-keyword">and</span> <span class="hljs-keyword">the</span> dark sky <span class="hljs-keyword">in</span> <span class="hljs-keyword">the</span> background. The astronaut <span class="hljs-keyword">is</span> holding a large egg <span class="hljs-keyword">that</span> has cracked open, revealing a small creature inside. The scene <span class="hljs-keyword">is</span> imaginative <span class="hljs-keyword">and</span> playful, combining elements <span class="hljs-keyword">of</span> <span class="hljs-literal">space</span> exploration <span class="hljs-keyword">with</span> a whimsical twist involving <span class="hljs-keyword">the</span> egg <span class="hljs-keyword">and</span> <span class="hljs-keyword">the</span> creature.<!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> openai <span class="hljs-keyword">import</span> OpenAI
client = OpenAI(base_url=<span class="hljs-string">"http://localhost:8000/v1"</span>, api_key=<span class="hljs-string">"<random_string>"</span>)
response = client.responses.create(
model=<span class="hljs-string">"Qwen/Qwen2.5-VL-7B-Instruct"</span>,
<span class="hljs-built_in">input</span>=[
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"input_text"</span>, <span class="hljs-string">"text"</span>: <span class="hljs-string">"What's in this image?"</span>},
{
<span class="hljs-string">"type"</span>: <span class="hljs-string">"input_image"</span>,
<span class="hljs-string">"image_url"</span>: <span class="hljs-string">"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/astronaut.jpg"</span>,
},
],
max_output_tokens=<span class="hljs-number">256</span>,
stream=<span class="hljs-literal">True</span>,
)
<span class="hljs-keyword">for</span> event <span class="hljs-keyword">in</span> response:
<span class="hljs-keyword">if</span> <span class="hljs-built_in">hasattr</span>(event, <span class="hljs-string">"delta"</span>) <span class="hljs-keyword">and</span> event.delta:
<span class="hljs-built_in">print</span>(event.delta, end=<span class="hljs-string">""</span>)<!----></pre></div><!----> <p>Client <a href="https://platform.openai.com/docs/quickstart" rel="nofollow">OpenAI</a> trả về một chuỗi đã được in ra.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-xmp "><!---->The image depicts an astronaut <span class="hljs-keyword">in</span> a <span class="hljs-literal">space</span> suit standing <span class="hljs-keyword">on</span> what appears <span class="hljs-keyword">to</span> be <span class="hljs-keyword">the</span> surface <span class="hljs-keyword">of</span> <span class="hljs-keyword">the</span> moon, <span class="hljs-keyword">given</span> <span class="hljs-keyword">the</span> barren, rocky landscape <span class="hljs-keyword">and</span> <span class="hljs-keyword">the</span> dark sky <span class="hljs-keyword">in</span> <span class="hljs-keyword">the</span> background. The astronaut <span class="hljs-keyword">is</span> holding a large egg <span class="hljs-keyword">that</span> has cracked open, revealing a small creature inside. The scene <span class="hljs-keyword">is</span> imaginative <span class="hljs-keyword">and</span> playful, combining elements <span class="hljs-keyword">of</span> <span class="hljs-literal">space</span> exploration <span class="hljs-keyword">with</span> a whimsical twist involving <span class="hljs-keyword">the</span> egg <span class="hljs-keyword">and</span> <span class="hljs-keyword">the</span> creature.<!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->curl http://localhost:8000/v1/responses \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen/Qwen2.5-VL-7B-Instruct",
"input": [
{"type": "input_text", "text": "What'\''s in this image?"},
{"type": "input_image", "image_url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/astronaut.jpg"}
],
"max_output_tokens": 256,
"stream": false
}'<!----></pre></div><!----> <p>Lệnh trả về một chuỗi JSON.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-json "><!----><span class="hljs-punctuation">{</span>
<span class="hljs-attr">"id"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"resp_f47ac10b-58cc-4372-a567-0e02b2c3d479"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"created_at"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1776258580.7114341</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"model"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Qwen/Qwen2.5-VL-7B-Instruct@main"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"object"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"response"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"output"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
<span class="hljs-punctuation">{</span>
<span class="hljs-attr">"id"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"msg_f47ac10b-58cc-4372-a567-0e02b2c3d479"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"content"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
<span class="hljs-punctuation">{</span>
<span class="hljs-attr">"annotations"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"text"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"The image depicts an astronaut in a space suit standing on what appears to be the surface of the moon [...]"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"output_text"</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"role"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"assistant"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"status"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"completed"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"message"</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"status"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"completed"</span>
<span class="hljs-punctuation">}</span><!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->curl http://localhost:8000/v1/responses \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen/Qwen2.5-VL-7B-Instruct",
"stream": true,
"input": [
{"type": "input_text", "text": "What'\''s in this image?"},
{"type": "input_image", "image_url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/astronaut.jpg"}
],
"max_output_tokens": 256
}'<!----></pre></div><!----> <p>Lệnh trả về một luồng các khối dữ liệu.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->data: {"response":{"id":"resp_f47ac10b-58cc-4372-a567-0e02b2c3d479","created_at":1776258580.7114341,"model":"Qwen/Qwen2.5-VL-7B-Instruct@main","object":"response","output":[],"status":"queued"},"sequence_number":0,"type":"response.created"}
data: {"content_index":0,"delta":"The ","item_id":"msg_f47ac10b","output_index":0,"sequence_number":5,"type":"response.output_text.delta"}
(...)<!----></pre></div><!----><!----></div><!----><!----></div><!----> <!--[2--><h3 class="relative group"><a id="audio-based-responses" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#audio-based-responses"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Phản hồi dựa trên âm thanh<!----></span></h3><!--]--><!----> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><!--[--><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">openai</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">openai (dòng)</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">curl</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">curl (dòng)</div><!--]--></div> <div class="language-select"><div><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">import</span> base64
<span class="hljs-keyword">from</span> huggingface_hub.utils <span class="hljs-keyword">import</span> httpx
<span class="hljs-keyword">from</span> openai <span class="hljs-keyword">import</span> OpenAI
client = OpenAI(base_url=<span class="hljs-string">"http://localhost:8000/v1"</span>, api_key=<span class="hljs-string">"<random_string>"</span>)
audio_url = <span class="hljs-string">"https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/obama_first_45_secs.mp3"</span>
audio_b64 = base64.b64encode(httpx.get(audio_url, follow_redirects=<span class="hljs-literal">True</span>).content).decode()
response = client.responses.create(
model=<span class="hljs-string">"google/gemma-4-E2B-it"</span>,
<span class="hljs-built_in">input</span>=[
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"input_text"</span>, <span class="hljs-string">"text"</span>: <span class="hljs-string">"Transcribe this audio."</span>},
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"input_audio"</span>, <span class="hljs-string">"input_audio"</span>: {<span class="hljs-string">"data"</span>: audio_b64, <span class="hljs-string">"format"</span>: <span class="hljs-string">"mp3"</span>}},
],
max_output_tokens=<span class="hljs-number">256</span>,
stream=<span class="hljs-literal">False</span>,
)
<span class="hljs-built_in">print</span>(response.output[<span class="hljs-number">0</span>].content[<span class="hljs-number">0</span>].text)<!----></pre></div><!----> <p>Client <a href="https://platform.openai.com/docs/quickstart" rel="nofollow">OpenAI</a> trả về một chuỗi đã được in ra.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class=" "><!---->This week, I traveled <span class="hljs-keyword">to</span> Chicago <span class="hljs-keyword">to</span> deliver <span class="hljs-keyword">my</span> final farewell address <span class="hljs-keyword">to</span> <span class="hljs-keyword">the</span> nation, following <span class="hljs-keyword">in</span> <span class="hljs-keyword">the</span> tradition <span class="hljs-keyword">of</span> presidents <span class="hljs-keyword">before</span> <span class="hljs-keyword">me</span>. It was an opportunity <span class="hljs-keyword">to</span> <span class="hljs-built_in">say</span> thank you. Whether we've seen eye <span class="hljs-keyword">to</span> eye <span class="hljs-keyword">or</span> rarely agreed <span class="hljs-keyword">at</span> all, <span class="hljs-keyword">my</span> conversations <span class="hljs-keyword">with</span> you, <span class="hljs-keyword">the</span> American people, <span class="hljs-keyword">in</span> living rooms <span class="hljs-keyword">and</span> schools, <span class="hljs-keyword">at</span> farms <span class="hljs-keyword">and</span> <span class="hljs-keyword">on</span> factory floors, <span class="hljs-keyword">at</span> diners, <span class="hljs-keyword">and</span> <span class="hljs-keyword">on</span> distant military outposts, all these conversations are what have kept <span class="hljs-keyword">me</span> honest.<!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">import</span> base64
<span class="hljs-keyword">from</span> huggingface_hub.utils <span class="hljs-keyword">import</span> httpx
<span class="hljs-keyword">from</span> openai <span class="hljs-keyword">import</span> OpenAI
client = OpenAI(base_url=<span class="hljs-string">"http://localhost:8000/v1"</span>, api_key=<span class="hljs-string">"<random_string>"</span>)
audio_url = <span class="hljs-string">"https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/obama_first_45_secs.mp3"</span>
audio_b64 = base64.b64encode(httpx.get(audio_url, follow_redirects=<span class="hljs-literal">True</span>).content).decode()
response = client.responses.create(
model=<span class="hljs-string">"google/gemma-4-E2B-it"</span>,
<span class="hljs-built_in">input</span>=[
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"input_text"</span>, <span class="hljs-string">"text"</span>: <span class="hljs-string">"Transcribe this audio."</span>},
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"input_audio"</span>, <span class="hljs-string">"input_audio"</span>: {<span class="hljs-string">"data"</span>: audio_b64, <span class="hljs-string">"format"</span>: <span class="hljs-string">"mp3"</span>}},
],
}
],
max_output_tokens=<span class="hljs-number">256</span>,
stream=<span class="hljs-literal">True</span>,
)
<span class="hljs-keyword">for</span> event <span class="hljs-keyword">in</span> response:
<span class="hljs-keyword">if</span> <span class="hljs-built_in">hasattr</span>(event, <span class="hljs-string">"delta"</span>) <span class="hljs-keyword">and</span> event.delta:
<span class="hljs-built_in">print</span>(event.delta, end=<span class="hljs-string">""</span>)<!----></pre></div><!----> <p>Client <a href="https://platform.openai.com/docs/quickstart" rel="nofollow">OpenAI</a> trả về một chuỗi đã được in ra.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class=" "><!---->This week, I traveled <span class="hljs-keyword">to</span> Chicago <span class="hljs-keyword">to</span> deliver <span class="hljs-keyword">my</span> final farewell address <span class="hljs-keyword">to</span> <span class="hljs-keyword">the</span> nation, following <span class="hljs-keyword">in</span> <span class="hljs-keyword">the</span> tradition <span class="hljs-keyword">of</span> presidents <span class="hljs-keyword">before</span> <span class="hljs-keyword">me</span>. It was an opportunity <span class="hljs-keyword">to</span> <span class="hljs-built_in">say</span> thank you. Whether we've seen eye <span class="hljs-keyword">to</span> eye <span class="hljs-keyword">or</span> rarely agreed <span class="hljs-keyword">at</span> all, <span class="hljs-keyword">my</span> conversations <span class="hljs-keyword">with</span> you, <span class="hljs-keyword">the</span> American people, <span class="hljs-keyword">in</span> living rooms <span class="hljs-keyword">and</span> schools, <span class="hljs-keyword">at</span> farms <span class="hljs-keyword">and</span> <span class="hljs-keyword">on</span> factory floors, <span class="hljs-keyword">at</span> diners, <span class="hljs-keyword">and</span> <span class="hljs-keyword">on</span> distant military outposts, all these conversations are what have kept <span class="hljs-keyword">me</span> honest.<!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!----><span class="hljs-meta prompt_"># </span><span class="language-bash">First, base64-encode the audio file and build the JSON payload</span>
AUDIO_B64=$(curl -sL https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/obama_first_45_secs.mp3 | base64 -w 0)
cat <<EOF > /tmp/audio_request.json
{
"model": "google/gemma-4-E2B-it",
"input": [
{"type": "input_text", "text": "Transcribe this audio."},
{"type": "input_audio", "input_audio": {"data": "$AUDIO_B64", "format": "mp3"}}
],
"max_output_tokens": 256,
"stream": false
}
EOF
curl http://localhost:8000/v1/responses \
-H "Content-Type: application/json" \
-d @/tmp/audio_request.json<!----></pre></div><!----> <p>Lệnh trả về một chuỗi JSON.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-json "><!----><span class="hljs-punctuation">{</span>
<span class="hljs-attr">"id"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"resp_a1b2c3d4-e5f6-7890-abcd-ef1234567890"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"created_at"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1776258580.7114341</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"model"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"google/gemma-4-E2B-it@main"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"object"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"response"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"output"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
<span class="hljs-punctuation">{</span>
<span class="hljs-attr">"id"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"msg_a1b2c3d4-e5f6-7890-abcd-ef1234567890"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"content"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
<span class="hljs-punctuation">{</span>
<span class="hljs-attr">"annotations"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"text"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"This week, I traveled to Chicago to deliver my final farewell address to the nation [...]"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"output_text"</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"role"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"assistant"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"status"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"completed"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"message"</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"status"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"completed"</span>
<span class="hljs-punctuation">}</span><!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!----><span class="hljs-meta prompt_"># </span><span class="language-bash">First, base64-encode the audio file and build the JSON payload</span>
AUDIO_B64=$(curl -sL https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/obama_first_45_secs.mp3 | base64 -w 0)
cat <<EOF > /tmp/audio_request.json
{
"model": "google/gemma-4-E2B-it",
"stream": true,
"input": [
{"type": "input_text", "text": "Transcribe this audio."},
{"type": "input_audio", "input_audio": {"data": "$AUDIO_B64", "format": "mp3"}}
],
"max_output_tokens": 256
}
EOF
curl http://localhost:8000/v1/responses \
-H "Content-Type: application/json" \
-d @/tmp/audio_request.json<!----></pre></div><!----> <p>Lệnh trả về một luồng các khối dữ liệu.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->data: {"response":{"id":"resp_a1b2c3d4-e5f6-7890-abcd-ef1234567890","created_at":1776258580.7114341,"model":"google/gemma-4-E2B-it@main","object":"response","output":[],"status":"queued"},"sequence_number":0,"type":"response.created"}
data: {"content_index":0,"delta":"This ","item_id":"msg_a1b2c3d4","output_index":0,"sequence_number":5,"type":"response.output_text.delta"}
(...)<!----></pre></div><!----><!----></div><!----><!----></div><!----> <blockquote class="warning"><p>Loại nội dung <code>audio_url</code> là một phần mở rộng không thuộc chuẩn OpenAI và có thể thay đổi trong các phiên bản tương lai.</p></blockquote> <p>Bạn cũng có thể truyền audio bằng URL với kiểu nội dung <code>audio_url</code> để bỏ qua mã hóa base64.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!---->response = client.responses.create(
model=<span class="hljs-string">"google/gemma-4-E2B-it"</span>,
<span class="hljs-built_in">input</span>=[
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"input_text"</span>, <span class="hljs-string">"text"</span>: <span class="hljs-string">"Transcribe this audio."</span>},
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"audio_url"</span>, <span class="hljs-string">"audio_url"</span>: {<span class="hljs-string">"url"</span>: <span class="hljs-string">"https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/obama_first_45_secs.mp3"</span>}},
],
max_output_tokens=<span class="hljs-number">256</span>,
stream=<span class="hljs-literal">False</span>,
)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="video-based-responses" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#video-based-responses"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Phản hồi dựa trên video<!----></span></h3><!--]--><!----> <blockquote class="warning"><p>Loại nội dung <code>video_url</code> là một phần mở rộng không thuộc chuẩn OpenAI và có thể thay đổi trong các phiên bản tương lai.</p></blockquote> <blockquote class="tip"><p>Xử lý video yêu cầu <a href="https://github.com/pytorch/torchcodec" rel="nofollow">torchcodec</a>. Cài đặt bằng <code>pip install torchcodec</code>.</p></blockquote> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><!--[--><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">openai</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">openai (dòng)</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">curl</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">curl (dòng)</div><!--]--></div> <div class="language-select"><div><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> openai <span class="hljs-keyword">import</span> OpenAI
client = OpenAI(base_url=<span class="hljs-string">"http://localhost:8000/v1"</span>, api_key=<span class="hljs-string">"<random_string>"</span>)
response = client.responses.create(
model=<span class="hljs-string">"google/gemma-4-E2B-it"</span>,
<span class="hljs-built_in">input</span>=[
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"input_text"</span>, <span class="hljs-string">"text"</span>: <span class="hljs-string">"What is happening in the video and what is the song about?"</span>},
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"video_url"</span>, <span class="hljs-string">"video_url"</span>: {<span class="hljs-string">"url"</span>: <span class="hljs-string">"https://huggingface.co/datasets/merve/vlm_test_images/resolve/main/concert.mp4"</span>}},
],
max_output_tokens=<span class="hljs-number">256</span>,
stream=<span class="hljs-literal">False</span>,
)
<span class="hljs-built_in">print</span>(response.output[<span class="hljs-number">0</span>].content[<span class="hljs-number">0</span>].text)<!----></pre></div><!----> <p>Client <a href="https://platform.openai.com/docs/quickstart" rel="nofollow">OpenAI</a> trả về một chuỗi đã được in ra.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class=" "><!---->The video captures a live music performance at a music festival<span class="hljs-built_in"> or </span>a large concert. There are several musicians on stage, including a central figure playing an acoustic guitar<span class="hljs-built_in"> and </span>singing. The foreground is<span class="hljs-built_in"> filled </span>with the backs of the audience, indicating a large crowd watching the show. The stage is dramatically lit with bright spotlights<span class="hljs-built_in"> and </span>blue<span class="hljs-built_in"> and </span>white stage lighting, with haze<span class="hljs-built_in"> and </span>smoke creating an immersive atmosphere.
The lyrics of the song are: <span class="hljs-string">"I don't care 'bout street, from that fresh street, 'cause there's no problem, another one I want to be, in the storm..."</span><!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> openai <span class="hljs-keyword">import</span> OpenAI
client = OpenAI(base_url=<span class="hljs-string">"http://localhost:8000/v1"</span>, api_key=<span class="hljs-string">"<random_string>"</span>)
response = client.responses.create(
model=<span class="hljs-string">"google/gemma-4-E2B-it"</span>,
<span class="hljs-built_in">input</span>=[
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"input_text"</span>, <span class="hljs-string">"text"</span>: <span class="hljs-string">"What is happening in the video and what is the song about?"</span>},
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"video_url"</span>, <span class="hljs-string">"video_url"</span>: {<span class="hljs-string">"url"</span>: <span class="hljs-string">"https://huggingface.co/datasets/merve/vlm_test_images/resolve/main/concert.mp4"</span>}},
],
max_output_tokens=<span class="hljs-number">256</span>,
stream=<span class="hljs-literal">True</span>,
)
<span class="hljs-keyword">for</span> event <span class="hljs-keyword">in</span> response:
<span class="hljs-keyword">if</span> <span class="hljs-built_in">hasattr</span>(event, <span class="hljs-string">"delta"</span>) <span class="hljs-keyword">and</span> event.delta:
<span class="hljs-built_in">print</span>(event.delta, end=<span class="hljs-string">""</span>)<!----></pre></div><!----> <p>Client <a href="https://platform.openai.com/docs/quickstart" rel="nofollow">OpenAI</a> trả về một chuỗi đã được in ra.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class=" "><!---->The video captures a live music performance at a music festival<span class="hljs-built_in"> or </span>a large concert. There are several musicians on stage, including a central figure playing an acoustic guitar<span class="hljs-built_in"> and </span>singing. The foreground is<span class="hljs-built_in"> filled </span>with the backs of the audience, indicating a large crowd watching the show. The stage is dramatically lit with bright spotlights<span class="hljs-built_in"> and </span>blue<span class="hljs-built_in"> and </span>white stage lighting, with haze<span class="hljs-built_in"> and </span>smoke creating an immersive atmosphere.
The lyrics of the song are: <span class="hljs-string">"I don't care 'bout street, from that fresh street, 'cause there's no problem, another one I want to be, in the storm..."</span><!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->curl http://localhost:8000/v1/responses \
-H "Content-Type: application/json" \
-d '{
"model": "google/gemma-4-E2B-it",
"input": [
{"type": "input_text", "text": "What is happening in the video and what is the song about?"},
{"type": "video_url", "video_url": {"url": "https://huggingface.co/datasets/merve/vlm_test_images/resolve/main/concert.mp4"}}
],
"max_output_tokens": 256,
"stream": false
}'<!----></pre></div><!----> <p>Lệnh trả về một chuỗi JSON.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-json "><!----><span class="hljs-punctuation">{</span>
<span class="hljs-attr">"id"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"resp_b2c3d4e5-f6a7-8901-bcde-f12345678901"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"created_at"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1776258580.7114341</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"model"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"google/gemma-4-E2B-it@main"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"object"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"response"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"output"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
<span class="hljs-punctuation">{</span>
<span class="hljs-attr">"id"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"msg_b2c3d4e5-f6a7-8901-bcde-f12345678901"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"content"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
<span class="hljs-punctuation">{</span>
<span class="hljs-attr">"annotations"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"text"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"The video captures a live music performance at a music festival or a large concert [...]"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"output_text"</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"role"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"assistant"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"status"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"completed"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"message"</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"status"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"completed"</span>
<span class="hljs-punctuation">}</span><!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->curl http://localhost:8000/v1/responses \
-H "Content-Type: application/json" \
-d '{
"model": "google/gemma-4-E2B-it",
"stream": true,
"input": [
{"type": "input_text", "text": "What is happening in the video and what is the song about?"},
{"type": "video_url", "video_url": {"url": "https://huggingface.co/datasets/merve/vlm_test_images/resolve/main/concert.mp4"}}
],
"max_output_tokens": 256
}'<!----></pre></div><!----> <p>Lệnh trả về một luồng các khối dữ liệu.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->data: {"response":{"id":"resp_b2c3d4e5-f6a7-8901-bcde-f12345678901","created_at":1776258580.7114341,"model":"google/gemma-4-E2B-it@main","object":"response","output":[],"status":"queued"},"sequence_number":0,"type":"response.created"}
data: {"content_index":0,"delta":"Based ","item_id":"msg_b2c3d4e5","output_index":0,"sequence_number":5,"type":"response.output_text.delta"}
(...)<!----></pre></div><!----><!----></div><!----><!----></div><!----> <!--[2--><h3 class="relative group"><a id="responses" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#responses"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Cuộc trò chuyện nhiều lượt<!----></span></h3><!--]--><!----> <p>Đối với các cuộc trò chuyện nhiều lượt, hãy truyền một danh sách các tin nhắn có các khóa <code>role</code> trong trường <code>input</code>. Giống như tất cả các máy chủ tương thích với OpenAI, API không có trạng thái, do đó mỗi yêu cầu phải chứa lịch sử trò chuyện đầy đủ.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> openai <span class="hljs-keyword">import</span> OpenAI
client = OpenAI(base_url=<span class="hljs-string">"http://localhost:8000/v1"</span>, api_key=<span class="hljs-string">"<random_string>"</span>)
response = client.responses.create(
model=<span class="hljs-string">"Qwen/Qwen2.5-0.5B-Instruct"</span>,
<span class="hljs-built_in">input</span>=[
{<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>, <span class="hljs-string">"content"</span>: <span class="hljs-string">"What is the capital of France?"</span>},
{<span class="hljs-string">"role"</span>: <span class="hljs-string">"assistant"</span>, <span class="hljs-string">"content"</span>: <span class="hljs-string">"The capital of France is Paris."</span>},
{<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>, <span class="hljs-string">"content"</span>: <span class="hljs-string">"How many people live there?"</span>},
],
max_output_tokens=<span class="hljs-number">256</span>,
stream=<span class="hljs-literal">False</span>,
)
<span class="hljs-built_in">print</span>(response.output[<span class="hljs-number">0</span>].content[<span class="hljs-number">0</span>].text)<!----></pre></div><!----> <p>Câu hỏi tiếp theo “Có bao nhiêu người sống ở đó?” dựa vào ngữ cảnh trước đó, vì vậy mô hình trả lời về Paris.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class=" "><!----><span class="hljs-attribute">As</span> of <span class="hljs-number">2021</span>, Paris has a population of approximately <span class="hljs-number">2</span>.<span class="hljs-number">8</span> million people.<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="v1systemone" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#v1systemone"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->v1/systemone<!----></span></h2><!--]--><!----> <p>Điểm cuối <code>v1/systemone</code> trả lời các câu hỏi có kiểu dữ liệu về một <code>state</code> chung, tuân theo <a href="https://api.typesafe.ai/openapi.json" rel="nofollow">API System One của TypeSafe</a>. Hãy sử dụng nó cho các quyết định như định tuyến một vé hỗ trợ, đánh giá mức độ khẩn cấp, hoặc chọn một hành động.</p> <p>Các yêu cầu được xử lý bởi <a href="/docs/transformers/main/vi/main_classes/pipelines#transformers.DecisionPipeline">DecisionPipeline</a> bao quanh model được phục vụ, do đó endpoint và pipeline xây dựng cùng các prompt và trả về cùng các câu trả lời. Model chấm điểm các câu trả lời được phép mà không tạo ra văn bản, trong một lần forward pass cho mỗi prompt. Endpoint này hỗ trợ các model tự hồi quy có chat template;</p> <p>Gửi một mô hình, trạng thái cần đánh giá và các câu hỏi được đặt tên theo tên:</p> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><!--[--><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">curl</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">TypeSafe (TypeScript)</div><!--]--></div> <div class="language-select"><div><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->curl http://localhost:8000/v1/systemone \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen/Qwen2.5-1.5B-Instruct",
"state": "Our integration has been failing for 3 days and we are losing sales!",
"questions": {
"team": {
"type": "choice",
"instructions": "Which team handles this?",
"criteria": {"billing": null, "technical": "Bugs or integration problems"}
},
"urgency": {
"type": "score",
"instructions": "How urgent is this ticket?",
"criteria": ["low", "medium", "high"]
},
"upset": {"type": "noul", "instructions": "Is the customer upset?"}
}
}'<!----></pre></div><!----></div><!----> <div hidden=""><p>Cài đặt <a href="https://github.com/typesafe-ai/typesafe-sdk-js" rel="nofollow">TypeSafe SDK</a> cho Node.js 20 trở lên:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->npm install @typesafe-ai/sdk<!----></pre></div><!----> <p>Đặt <code>baseURL</code> là gốc của máy chủ; SDK sẽ thêm <code>/v1/systemone</code>. SDK yêu cầu một khóa API, nhưng một giá trị thay thế vẫn hoạt động với máy chủ cục bộ.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-typescript "><!----><span class="hljs-keyword">import</span> { choice, noul, score, <span class="hljs-title class_">TypeSafeClient</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">"@typesafe-ai/sdk"</span>;
<span class="hljs-keyword">const</span> client = <span class="hljs-keyword">new</span> <span class="hljs-title class_">TypeSafeClient</span>({
<span class="hljs-attr">baseURL</span>: <span class="hljs-string">"http://localhost:8000"</span>,
<span class="hljs-attr">apiKey</span>: <span class="hljs-string">"<random_string>"</span>,
});
<span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span> client.<span class="hljs-title function_">systemOne</span>({
<span class="hljs-attr">model</span>: <span class="hljs-string">"Qwen/Qwen2.5-1.5B-Instruct"</span>,
<span class="hljs-attr">state</span>: <span class="hljs-string">"Our integration has been failing for 3 days and we are losing sales!"</span>,
<span class="hljs-attr">questions</span>: {
<span class="hljs-attr">team</span>: <span class="hljs-title function_">choice</span>(<span class="hljs-string">"Which team handles this?"</span>, {
<span class="hljs-attr">billing</span>: <span class="hljs-literal">null</span>,
<span class="hljs-attr">technical</span>: <span class="hljs-string">"Bugs or integration problems"</span>,
}),
<span class="hljs-attr">urgency</span>: <span class="hljs-title function_">score</span>(<span class="hljs-string">"How urgent is this ticket?"</span>, [<span class="hljs-string">"low"</span>, <span class="hljs-string">"medium"</span>, <span class="hljs-string">"high"</span>]),
<span class="hljs-attr">upset</span>: <span class="hljs-title function_">noul</span>(<span class="hljs-string">"Is the customer upset?"</span>),
},
});
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(response.<span class="hljs-property">answers</span>.<span class="hljs-property">team</span>.<span class="hljs-property">choice</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(response.<span class="hljs-property">answers</span>.<span class="hljs-property">urgency</span>.<span class="hljs-property">score</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(response.<span class="hljs-property">answers</span>.<span class="hljs-property">upset</span>.<span class="hljs-property">noul</span>);<!----></pre></div><!----> <p>Client in ra đội được chọn, điểm khẩn cấp và xác suất khách hàng đang bực bội. Ví dụ:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->technical
1.6739138662815094
0.851952850818634<!----></pre></div><!----><!----></div><!----><!----></div><!----> <p>Câu trả lời sử dụng cùng tên câu hỏi. Ví dụ:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-json "><!----><span class="hljs-punctuation">{</span>
<span class="hljs-attr">"model"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Qwen/Qwen2.5-1.5B-Instruct"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"answers"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
<span class="hljs-attr">"team"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span><span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"choice"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"choice"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"technical"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"probabilities"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span><span class="hljs-attr">"billing"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0.000006962256520637311</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"technical"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0.999993085861206</span><span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"confidence"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0.9999861717224121</span><span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"urgency"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span><span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"score"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"score"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1.6739138662815094</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"legend"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span><span class="hljs-attr">"0"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"low"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"1"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"medium"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"2"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"high"</span><span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"probabilities"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span><span class="hljs-attr">"0"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0.0031353600788861513</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"1"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0.31981542706489563</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"2"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0.6770492196083069</span><span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"confidence"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0.5108707791659981</span><span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"upset"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span><span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"noul"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"noul"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0.851952850818634</span><span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"usage"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span><span class="hljs-attr">"input_tokens"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">213</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"output_tokens"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span><!----></pre></div><!----> <table><thead><tr><th align="left">Loại câu hỏi</th><th align="left"><code>criteria</code></th><th align="left">Câu trả lời</th></tr></thead><tbody><tr><td align="left"><code>choice</code></td><td align="left">Tên các tùy chọn được ánh xạ đến mô tả, có thể là <code>null</code>.</td><td align="left">Phương án có khả năng cao nhất, xác suất của các phương án và độ tin cậy.</td></tr><tr><td align="left"><code>score</code></td><td align="left">Mô tả các cấp độ được sắp xếp từ thấp đến cao.</td><td align="left">Chỉ số mức có trọng số xác suất, các xác suất, chú giải và độ tin cậy.</td></tr><tr><td align="left"><code>noul</code></td><td align="left">Mô tả tùy chọn cho <code>true</code> và <code>false</code>.</td><td align="left">Xác suất của <code>true</code>.</td></tr></tbody></table> <p><code>state</code>, tùy chọn <code>instructions</code>, và mô tả có thể chứa văn bản hoặc các đối tượng và mảng JSON. Mỗi yêu cầu chấp nhận tối đa 64 câu hỏi, với ít nhất một lựa chọn hoặc mức điểm cho mỗi câu hỏi và tối đa một cho mỗi nhãn câu trả lời (26 với các nhãn mặc định <code>A</code>–<code>Z</code>).</p> <p>Các câu hỏi được xác thực giống như của <a href="/docs/transformers/main/vi/main_classes/pipelines#transformers.DecisionPipeline">DecisionPipeline</a>, trước khi mô hình được nạp. Một yêu cầu không hợp lệ sẽ trả về <code>422</code>, trong đó thông báo liệt kê mọi câu hỏi không hợp lệ, mỗi câu kèm theo một ví dụ hợp lệ, và gợi ý các kiểu hoặc trường tương tự cho các trường hợp đánh sai chính tả:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-text "><!---->Question 'team': expected a `type` among noul, choice, score, got type 'Choice'. Did you mean 'choice'?
Question 'upset': unknown field 'instruction'. Did you mean 'instructions'? A noul question looks like {"type": "noul", ...}<!----></pre></div><!----> <p>Đối với các model có khả năng thị giác, hãy thêm <code>assets</code>, giống như trong System One API: tối đa bốn hình ảnh được gán khóa bằng một placeholder do bạn chọn (UUID là an toàn nhất). Bất cứ đâu một placeholder xuất hiện trong chuỗi của <code>state</code>, <code>instructions</code> hoặc <code>criteria</code>, model sẽ nhìn thấy hình ảnh tại đó. Một asset là <code>{"type": "image", "format": "png", "data_base64": "..."}</code>; các URL hình ảnh từ xa không được chấp nhận. Model cần một processor xử lý hình ảnh.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-json "><!----><span class="hljs-punctuation">{</span>
<span class="hljs-attr">"model"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Qwen/Qwen2.5-VL-3B-Instruct"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"state"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"The customer sent 6f1c0e2a and asks if it is damaged."</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"assets"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span><span class="hljs-attr">"6f1c0e2a"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span><span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"image"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"format"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"png"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"data_base64"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"iVBORw0..."</span><span class="hljs-punctuation">}</span><span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"questions"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span><span class="hljs-attr">"damaged"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span><span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"noul"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"instructions"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Is the product in the photo damaged?"</span><span class="hljs-punctuation">}</span><span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="decision-configuration" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#decision-configuration"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Cấu hình quyết định<!----></span></h3><!--]--><!----> <p>Các mô hình quyết định hiện chưa có một tiêu chuẩn chung để lưu trữ prompt, nhãn câu trả lời và cài đặt hiệu chuẩn. Những yếu tố này khác nhau giữa các triển khai mô hình, do đó <code>--decision-config</code> cho phép bạn cung cấp chúng một cách tường minh.</p> <p>Mặc định, các câu hỏi sử dụng prompt dự phòng và chat template của mô hình với tính năng suy nghĩ bị tắt. Nhãn câu trả lời là <code>A</code>–<code>Z</code> cho các lựa chọn và điểm số, và <code>Yes</code>/<code>No</code> cho giá trị boolean. Xác suất là softmax trên các nhãn này ở nhiệt độ 1.</p> <p>Truyền <code>--decision-config</code> để ghi đè các giá trị mặc định này cho một mô hình:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->transformers serve openjev/openjev --decision-config decision.json<!----></pre></div><!----> <p>Tệp JSON hỗ trợ ba trường tùy chọn:</p> <table><thead><tr><th align="left">Trường</th><th align="left">Ý nghĩa</th></tr></thead><tbody><tr><td align="left"><code>labels</code></td><td align="left">Nhãn câu trả lời theo loại câu hỏi, theo thứ tự các lựa chọn (<code>true</code>, sau đó là <code>false</code> đối với <code>noul</code>). Mỗi nhãn phải là một token đơn lẻ riêng biệt và đã biết.</td></tr><tr><td align="left"><code>temperature</code></td><td align="left">Nhiệt độ dương theo loại câu hỏi, được áp dụng dưới dạng <code>softmax(label_logits / temperature)</code>.</td></tr><tr><td align="left"><code>chat_template</code></td><td align="left">Một mẫu Jinja inline thay thế cho toàn bộ prompt, bao gồm định dạng chat và tiền tố của trợ lý.</td></tr></tbody></table> <p>Ví dụ, để thay đổi nhãn và hiệu chuẩn trong khi giữ nguyên prompt mặc định:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-json "><!----><span class="hljs-punctuation">{</span>
<span class="hljs-attr">"labels"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span><span class="hljs-attr">"noul"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-string">"A"</span><span class="hljs-punctuation">,</span> <span class="hljs-string">"B"</span><span class="hljs-punctuation">]</span><span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"temperature"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span><span class="hljs-attr">"choice"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0.85</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"score"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0.85</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"noul"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1.5547129</span><span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span><!----></pre></div><!----> <p>Một template tùy chỉnh, ở đây dành cho Gemma 3, sẽ viết toàn bộ prompt và <code><|mark|></code> tại vị trí đọc câu trả lời. Các chuỗi chứa hình ảnh được truyền đến dưới dạng danh sách các phần văn bản và hình ảnh:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-jinja "><!----><span class="hljs-template-tag">{%- <span class="hljs-name">macro</span> text(x) -%}</span><span class="language-xml">
</span><span class="hljs-template-tag">{%- <span class="hljs-name"><span class="hljs-name">if</span></span> x is string or x is none %}</span><span class="hljs-template-variable">{{ x or "" }}</span><span class="hljs-template-tag">{% <span class="hljs-name"><span class="hljs-name">elif</span></span> x is mapping %}</span><span class="hljs-template-variable">{{ x|tojson }}</span><span class="hljs-template-tag">{% <span class="hljs-name"><span class="hljs-name">else</span></span> -%}</span><span class="language-xml">
</span><span class="hljs-template-tag">{%- <span class="hljs-name"><span class="hljs-name">for</span></span> part <span class="hljs-keyword">in</span> x %}</span><span class="hljs-template-tag">{% <span class="hljs-name"><span class="hljs-name">if</span></span> part.type == "image" %}</span><span class="language-xml"><span class="hljs-tag"><<span class="hljs-name">start_of_image</span>></span></span><span class="hljs-template-tag">{% <span class="hljs-name"><span class="hljs-name">else</span></span> %}</span><span class="hljs-template-variable">{{ part.text }}</span><span class="hljs-template-tag">{% <span class="hljs-name"><span class="hljs-name">endif</span></span> %}</span><span class="hljs-template-tag">{% <span class="hljs-name"><span class="hljs-name">endfor</span></span> -%}</span><span class="language-xml">
</span><span class="hljs-template-tag">{%- <span class="hljs-name"><span class="hljs-name">endif</span></span> -%}</span><span class="language-xml">
</span><span class="hljs-template-tag">{%- <span class="hljs-name">endmacro</span> -%}</span><span class="language-xml">
<span class="hljs-tag"><<span class="hljs-name">start_of_turn</span>></span>user
</span><span class="hljs-template-variable">{{ text(state) }}</span><span class="language-xml">
</span><span class="hljs-template-variable">{{ text(question.instructions) }}</span><span class="language-xml">
</span><span class="hljs-template-tag">{% <span class="hljs-name"><span class="hljs-name">for</span></span> option <span class="hljs-keyword">in</span> question.options %}</span><span class="hljs-template-variable">{{ question.labels[loop.index0] }}</span><span class="language-xml">. </span><span class="hljs-template-variable">{{ option }}</span><span class="language-xml">: </span><span class="hljs-template-variable">{{ text(question.descriptions[loop.index0]) }}</span><span class="language-xml">
</span><span class="hljs-template-tag">{% <span class="hljs-name"><span class="hljs-name">endfor</span></span> %}</span><span class="language-xml"><span class="hljs-tag"><<span class="hljs-name">end_of_turn</span>></span>
<span class="hljs-tag"><<span class="hljs-name">start_of_turn</span>></span>model
<|mark|></span><!----></pre></div><!----> <p>Viết trực tiếp vào <code>chat_template</code> trong tệp <code>--decision-config</code>, hoặc cung cấp dưới dạng <code>decision_template.jinja</code> đặt cạnh <code>decision.json</code> trong kho mô hình.</p> <!--[1--><h2 class="relative group"><a id="v1audiotranscriptions" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#v1audiotranscriptions"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->v1/audio/transcriptions<!----></span></h2><!--]--><!----> <p>Endpoint <code>v1/audio/transcriptions</code> chuyển văn bản từ giọng nói bằng cách sử dụng các mô hình speech-to-text. Nó tuân theo định dạng <a href="https://platform.openai.com/docs/api-reference/audio/createTranscription" rel="nofollow">Audio transcription API</a>.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->curl -X POST http://localhost:8000/v1/audio/transcriptions \
-H "Content-Type: multipart/form-data" \
-F "file=@/path/to/audio.wav" \
-F "model=openai/whisper-large-v3"<!----></pre></div><!----> <p>Lệnh trả về phản hồi sau.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->{
"text": "Transcribed text from the audio file",
}<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="v1models" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#v1models"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->v1/models<!----></span></h2><!--]--><!----> <p>Điểm cuối <code>v1/models</code> trả về các mô hình mà máy chủ có thể phục vụ ở định dạng tương thích OpenAI. Các công cụ bên thứ ba sử dụng điểm cuối này để khám phá các mô hình khả dụng.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->curl http://localhost:8000/v1/models<!----></pre></div><!----> <p>Những gì được liệt kê phụ thuộc vào cách máy chủ được khởi động.</p> <ul><li><code>transformers serve</code> tải các mô hình theo yêu cầu, vì vậy điểm cuối sẽ liệt kê các mô hình đã được tải xuống trong bộ nhớ đệm Hugging Face cục bộ của bạn, cũng như các mô hình đã được nạp nhưng không có trong bộ nhớ đệm, chẳng hạn như một mô hình được phục vụ từ đường dẫn cục bộ.</li> <li><code>transformers serve <model></code> chỉ trả lời cho model đó, vì vậy endpoint chỉ liệt kê riêng model này.</li></ul> <p>Tải xuống một mô hình để nó xuất hiện trong danh sách trước khi được nạp.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-bash "><!---->transformers download Qwen/Qwen2.5-0.5B-Instruct<!----></pre></div><!----> <p>Mỗi model có một <code>status</code>, không phải là một phần của định dạng OpenAI. Nó là <code>loaded</code> khi model đang nằm trong bộ nhớ, và <code>unloaded</code> khi nó đã được tải xuống và sẽ được nạp vào ở yêu cầu đầu tiên có nêu tên nó.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-json "><!----><span class="hljs-punctuation">{</span>
<span class="hljs-attr">"object"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"list"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"data"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
<span class="hljs-punctuation">{</span>
<span class="hljs-attr">"id"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Qwen/Qwen2.5-0.5B-Instruct"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"object"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"model"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"created"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1776183469</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"owned_by"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Qwen"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"status"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span><span class="hljs-attr">"value"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"unloaded"</span><span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">]</span>
<span class="hljs-punctuation">}</span><!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="loading-models" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#loading-models"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Tải mô hình<!----></span></h2><!--]--><!----> <p>Endpoint <code>/load_model</code> tải trước một model và truyền tiến trình qua <a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events" rel="nofollow">Server-Sent Events</a> (SSE). CLI <code>transformers chat</code> sử dụng nó tự động để người dùng thấy tiến trình tải xuống và nạp model thay vì một prompt treo. Hãy dùng nó để làm nóng model trước khi gửi các yêu cầu suy luận.</p> <!--[2--><h3 class="relative group"><a id="request" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#request"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Yêu cầu<!----></span></h3><!--]--><!----> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->curl -N -X POST http://localhost:8000/load_model \
-H "Content-Type: application/json" \
-d '{"model": "Qwen/Qwen2.5-0.5B-Instruct"}'<!----></pre></div><!----> <p>Trường <code>model</code> là định danh mô hình Hugging Face, tùy chọn có hậu tố <code>@revision</code> (<code>Qwen/Qwen2.5-0.5B-Instruct@main</code>). Nếu bỏ qua revision, mặc định sẽ là <code>main</code>.</p> <!--[2--><h3 class="relative group"><a id="response" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#response"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Phản hồi<!----></span></h3><!--]--><!----> <p>Phản hồi là một luồng SSE (<code>Content-Type: text/event-stream</code>). Mỗi khung là một đối tượng JSON trên dòng <code>data:</code>.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class=" "><!----><span class="hljs-symbol">data:</span> {<span class="hljs-string">"status"</span>: <span class="hljs-string">"loading"</span>, <span class="hljs-string">"model"</span>: <span class="hljs-string">"Qwen/Qwen2.5-0.5B-Instruct@main"</span>, <span class="hljs-string">"stage"</span>: <span class="hljs-string">"processor"</span>}<!----></pre></div><!----> <p>Mỗi sự kiện chứa ít nhất trường <code>status</code> và <code>model</code>. Các trường bổ sung phụ thuộc vào trạng thái.</p> <table><thead><tr><th>Trường</th><th>Có mặt khi</th><th>Mô tả</th></tr></thead><tbody><tr><td><code>status</code></td><td>Luôn luôn</td><td><code>loading</code>, <code>ready</code> hoặc <code>error</code></td></tr><tr><td><code>model</code></td><td>Luôn luôn</td><td>Canonical <code>model_id@revision</code></td></tr><tr><td><code>stage</code></td><td><code>status == "loading"</code></td><td>Một trong các <code>processor</code>, <code>config</code>, <code>download</code>, <code>weights</code> (xem các giai đoạn bên dưới)</td></tr><tr><td><code>progress</code></td><td>các giai đoạn <code>download</code> và <code>weights</code></td><td>Đối tượng có <code>current</code> và <code>total</code> (số nguyên hoặc null)</td></tr><tr><td><code>cached</code></td><td><code>status == "ready"</code></td><td><code>true</code> nếu mô hình đã nằm trong bộ nhớ</td></tr><tr><td><code>message</code></td><td><code>status == "error"</code></td><td>Mô tả lỗi</td></tr></tbody></table> <!--[2--><h3 class="relative group"><a id="stages" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#stages"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Các giai đoạn<!----></span></h3><!--]--><!----> <p>Quá trình tải diễn ra theo thứ tự qua các giai đoạn này. Một số giai đoạn có thể bị bỏ qua (<code>download</code> sẽ bị bỏ qua khi các tệp đã được lưu trong bộ nhớ đệm cục bộ).</p> <table><thead><tr><th>Giai đoạn</th><th>Đã có tiến triển chưa?</th><th>Mô tả</th></tr></thead><tbody><tr><td><code>processor</code></td><td>Không</td><td>Tải tokenizer/processor</td></tr><tr><td><code>config</code></td><td>Không</td><td>Tải cấu hình mô hình</td></tr><tr><td><code>download</code></td><td>Có (bít)</td><td>Tải tệp tin mô hình</td></tr><tr><td><code>weights</code></td><td>Có (các mục)</td><td>Tải các tensor trọng số vào bộ nhớ</td></tr></tbody></table> <p>Dòng kết thúc bằng đúng một sự kiện cuối, <code>ready</code> (thành công) hoặc <code>error</code> (thất bại).</p> <!--[1--><h2 class="relative group"><a id="timeout" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#timeout"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Timeout<!----></span></h2><!--]--><!----> <p><code>transformers serve</code> xử lý các yêu cầu cho bất kỳ model nào. Mỗi model được nạp theo yêu cầu và giữ trong bộ nhớ GPU. Các model sẽ tự động được gỡ bỏ sau 300 giây không hoạt động để giải phóng bộ nhớ GPU. Đặt <code>--model-timeout</code> thành một giá trị khác tính bằng giây, hoặc <code>-1</code> để tắt việc gỡ bỏ.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->transformers serve --model-timeout 400<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="loading-examples" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#loading-examples"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Đang tải các ví dụ<!----></span></h3><!--]--><!----> <p>Các ví dụ bên dưới hiển thị phản hồi cho một mô hình vừa được tải xuống, một mô hình được nạp từ bộ nhớ đệm cục bộ của bạn (bỏ qua giai đoạn tải xuống) và một mô hình đã có trong bộ nhớ.</p> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><!--[--><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">tải mới</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">các tệp được lưu trong bộ nhớ đệm</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">trong bộ nhớ</div><!--]--></div> <div class="language-select"><div><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class=" "><!----><span class="hljs-attribute">data</span>: {<span class="hljs-string">"status"</span>: <span class="hljs-string">"loading"</span>, <span class="hljs-string">"model"</span>: <span class="hljs-string">"org/model@main"</span>, <span class="hljs-string">"stage"</span>: <span class="hljs-string">"processor"</span>}
<span class="hljs-attribute">data</span>: {<span class="hljs-string">"status"</span>: <span class="hljs-string">"loading"</span>, <span class="hljs-string">"model"</span>: <span class="hljs-string">"org/model@main"</span>, <span class="hljs-string">"stage"</span>: <span class="hljs-string">"config"</span>}
<span class="hljs-attribute">data</span>: {<span class="hljs-string">"status"</span>: <span class="hljs-string">"loading"</span>, <span class="hljs-string">"model"</span>: <span class="hljs-string">"org/model@main"</span>, <span class="hljs-string">"stage"</span>: <span class="hljs-string">"download"</span>, <span class="hljs-string">"progress"</span>: {<span class="hljs-string">"current"</span>: <span class="hljs-number">0</span>, <span class="hljs-string">"total"</span>: <span class="hljs-number">269100000</span>}}
<span class="hljs-attribute">data</span>: {<span class="hljs-string">"status"</span>: <span class="hljs-string">"loading"</span>, <span class="hljs-string">"model"</span>: <span class="hljs-string">"org/model@main"</span>, <span class="hljs-string">"stage"</span>: <span class="hljs-string">"download"</span>, <span class="hljs-string">"progress"</span>: {<span class="hljs-string">"current"</span>: <span class="hljs-number">134600000</span>, <span class="hljs-string">"total"</span>: <span class="hljs-number">269100000</span>}}
<span class="hljs-attribute">data</span>: {<span class="hljs-string">"status"</span>: <span class="hljs-string">"loading"</span>, <span class="hljs-string">"model"</span>: <span class="hljs-string">"org/model@main"</span>, <span class="hljs-string">"stage"</span>: <span class="hljs-string">"download"</span>, <span class="hljs-string">"progress"</span>: {<span class="hljs-string">"current"</span>: <span class="hljs-number">269100000</span>, <span class="hljs-string">"total"</span>: <span class="hljs-number">269100000</span>}}
<span class="hljs-attribute">data</span>: {<span class="hljs-string">"status"</span>: <span class="hljs-string">"loading"</span>, <span class="hljs-string">"model"</span>: <span class="hljs-string">"org/model@main"</span>, <span class="hljs-string">"stage"</span>: <span class="hljs-string">"weights"</span>, <span class="hljs-string">"progress"</span>: {<span class="hljs-string">"current"</span>: <span class="hljs-number">1</span>, <span class="hljs-string">"total"</span>: <span class="hljs-number">272</span>}}
<span class="hljs-attribute">data</span>: {<span class="hljs-string">"status"</span>: <span class="hljs-string">"loading"</span>, <span class="hljs-string">"model"</span>: <span class="hljs-string">"org/model@main"</span>, <span class="hljs-string">"stage"</span>: <span class="hljs-string">"weights"</span>, <span class="hljs-string">"progress"</span>: {<span class="hljs-string">"current"</span>: <span class="hljs-number">272</span>, <span class="hljs-string">"total"</span>: <span class="hljs-number">272</span>}}
<span class="hljs-attribute">data</span>: {<span class="hljs-string">"status"</span>: <span class="hljs-string">"ready"</span>, <span class="hljs-string">"model"</span>: <span class="hljs-string">"org/model@main"</span>, <span class="hljs-string">"cached"</span>: false}<!----></pre></div><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class=" "><!----><span class="hljs-keyword">data</span>: {<span class="hljs-string">"status"</span>: <span class="hljs-string">"loading"</span>, <span class="hljs-string">"model"</span>: <span class="hljs-string">"org/model@main"</span>, <span class="hljs-string">"stage"</span>: <span class="hljs-string">"processor"</span>}
<span class="hljs-keyword">data</span>: {<span class="hljs-string">"status"</span>: <span class="hljs-string">"loading"</span>, <span class="hljs-string">"model"</span>: <span class="hljs-string">"org/model@main"</span>, <span class="hljs-string">"stage"</span>: <span class="hljs-string">"config"</span>}
<span class="hljs-keyword">data</span>: {<span class="hljs-string">"status"</span>: <span class="hljs-string">"loading"</span>, <span class="hljs-string">"model"</span>: <span class="hljs-string">"org/model@main"</span>, <span class="hljs-string">"stage"</span>: <span class="hljs-string">"weights"</span>, <span class="hljs-string">"progress"</span>: {<span class="hljs-string">"current"</span>: <span class="hljs-number">1</span>, <span class="hljs-string">"total"</span>: <span class="hljs-number">272</span>}}
<span class="hljs-keyword">data</span>: {<span class="hljs-string">"status"</span>: <span class="hljs-string">"loading"</span>, <span class="hljs-string">"model"</span>: <span class="hljs-string">"org/model@main"</span>, <span class="hljs-string">"stage"</span>: <span class="hljs-string">"weights"</span>, <span class="hljs-string">"progress"</span>: {<span class="hljs-string">"current"</span>: <span class="hljs-number">272</span>, <span class="hljs-string">"total"</span>: <span class="hljs-number">272</span>}}
<span class="hljs-keyword">data</span>: {<span class="hljs-string">"status"</span>: <span class="hljs-string">"ready"</span>, <span class="hljs-string">"model"</span>: <span class="hljs-string">"org/model@main"</span>, <span class="hljs-string">"cached"</span>: <span class="hljs-literal">false</span>}<!----></pre></div><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class=" "><!----><span class="hljs-keyword">data</span>: {<span class="hljs-string">"status"</span>: <span class="hljs-string">"ready"</span>, <span class="hljs-string">"model"</span>: <span class="hljs-string">"org/model@main"</span>, <span class="hljs-string">"cached"</span>: <span class="hljs-literal">true</span>}<!----></pre></div><!----></div><!----><!----></div><!----> <!--[1--><h2 class="relative group"><a id="tool-calling" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#tool-calling"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Gọi công cụ<!----></span></h2><!--]--><!----> <p>Máy chủ <code>transformers serve</code> hỗ trợ gọi hàm theo phong cách OpenAI. Các mô hình được huấn luyện cho việc sử dụng công cụ sẽ tạo ra các lời gọi hàm có cấu trúc để ứng dụng của bạn thực thi.</p> <blockquote class="note"><p>Tool calling hoạt động với bất kỳ model nào có tokenizer khai báo tool call tokens. Qwen và Gemma 4 hoạt động ngay lập tức. Mở một <a href="https://github.com/huggingface/transformers/issues/new/choose" rel="nofollow">issue</a> để yêu cầu hỗ trợ cho một model cụ thể.</p></blockquote> <p>Định nghĩa các công cụ dưới dạng danh sách các thông số hàm tuân theo định dạng OpenAI.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> json
<span class="hljs-keyword">from</span> openai <span class="hljs-keyword">import</span> OpenAI
client = OpenAI(base_url=<span class="hljs-string">"http://localhost:8000/v1"</span>, api_key=<span class="hljs-string">"<KEY>"</span>)
tools = [
{
<span class="hljs-string">"type"</span>: <span class="hljs-string">"function"</span>,
<span class="hljs-string">"function"</span>: {
<span class="hljs-string">"name"</span>: <span class="hljs-string">"get_weather"</span>,
<span class="hljs-string">"description"</span>: <span class="hljs-string">"Get the current weather in a location"</span>,
<span class="hljs-string">"parameters"</span>: {
<span class="hljs-string">"type"</span>: <span class="hljs-string">"object"</span>,
<span class="hljs-string">"properties"</span>: {
<span class="hljs-string">"location"</span>: {
<span class="hljs-string">"type"</span>: <span class="hljs-string">"string"</span>,
<span class="hljs-string">"description"</span>: <span class="hljs-string">"The city name, e.g. San Francisco"</span>
},
<span class="hljs-string">"unit"</span>: {
<span class="hljs-string">"type"</span>: <span class="hljs-string">"string"</span>,
<span class="hljs-string">"enum"</span>: [<span class="hljs-string">"celsius"</span>, <span class="hljs-string">"fahrenheit"</span>],
<span class="hljs-string">"description"</span>: <span class="hljs-string">"temperature unit"</span>
}
},
<span class="hljs-string">"required"</span>: [<span class="hljs-string">"location"</span>]
}
}
}
]<!----></pre></div><!----> <p>Tùy chỉnh quá trình tạo bằng cách truyền các tham số <a href="/docs/transformers/main/vi/main_classes/text_generation#transformers.GenerationConfig">GenerationConfig</a> vào đối số <code>extra_body</code> trong <a href="https://platform.openai.com/docs/api-reference/responses/create" rel="nofollow">create</a>.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!---->generation_config = {
<span class="hljs-string">"max_new_tokens"</span>: <span class="hljs-number">512</span>,
<span class="hljs-string">"temperature"</span>: <span class="hljs-number">0.7</span>,
<span class="hljs-string">"top_p"</span>: <span class="hljs-number">0.9</span>,
<span class="hljs-string">"top_k"</span>: <span class="hljs-number">50</span>,
<span class="hljs-string">"do_sample"</span>: <span class="hljs-literal">True</span>,
<span class="hljs-string">"repetition_penalty"</span>: <span class="hljs-number">1.1</span>,
<span class="hljs-string">"no_repeat_ngram_size"</span>: <span class="hljs-number">3</span>,
}
response = client.responses.create(
model=<span class="hljs-string">"Qwen/Qwen2.5-7B-Instruct"</span>,
instructions=<span class="hljs-string">"You are a helpful weather assistant. Use the get_weather tool to answer questions."</span>,
<span class="hljs-built_in">input</span>=<span class="hljs-string">"What's the weather like in San Francisco?"</span>,
tools=tools,
stream=<span class="hljs-literal">True</span>,
extra_body={<span class="hljs-string">"generation_config"</span>: json.dumps(generation_config)}
)
<span class="hljs-keyword">for</span> event <span class="hljs-keyword">in</span> response:
<span class="hljs-built_in">print</span>(event)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="multi-turn-tool-calling" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#multi-turn-tool-calling"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Gọi công cụ đa lượt<!----></span></h3><!--]--><!----> <p>Sau khi model trả về một tool call, hãy thực thi hàm cục bộ, sau đó gửi kết quả trở lại trong một yêu cầu tiếp theo để nhận câu trả lời cuối cùng từ model. Mẫu này có chút khác biệt giữa hai API. Xem <a href="https://developers.openai.com/api/docs/guides/function-calling?api-mode=chat" rel="nofollow">hướng dẫn function calling của OpenAI</a> để biết thông số kỹ thuật đầy đủ.</p> <p>Các ví dụ dưới đây tái sử dụng danh sách <code>tools</code> được định nghĩa ở trên.</p> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><!--[--><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">v1/chat/completions</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">v1/responses</div><!--]--></div> <div class="language-select"><div><p>Truyền kết quả công cụ dưới dạng thông điệp <code>role: "tool"</code> với <code>tool_call_id</code> tương ứng.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-comment"># Model returns a tool call</span>
messages = [{<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>, <span class="hljs-string">"content"</span>: <span class="hljs-string">"What's the weather like in San Francisco?"</span>}]
response = client.chat.completions.create(
model=<span class="hljs-string">"Qwen/Qwen2.5-7B-Instruct"</span>,
messages=messages,
tools=tools,
)
assistant_message = response.choices[<span class="hljs-number">0</span>].message
<span class="hljs-comment"># Execute the tool locally</span>
tool_call = assistant_message.tool_calls[<span class="hljs-number">0</span>]
result = {<span class="hljs-string">"temperature"</span>: <span class="hljs-number">22</span>, <span class="hljs-string">"condition"</span>: <span class="hljs-string">"sunny"</span>} <span class="hljs-comment"># your actual function call here</span>
<span class="hljs-comment"># Send the tool result back</span>
messages.append(assistant_message)
messages.append({
<span class="hljs-string">"role"</span>: <span class="hljs-string">"tool"</span>,
<span class="hljs-string">"tool_call_id"</span>: tool_call.<span class="hljs-built_in">id</span>,
<span class="hljs-string">"content"</span>: json.dumps(result),
})
final_response = client.chat.completions.create(
model=<span class="hljs-string">"Qwen/Qwen2.5-7B-Instruct"</span>,
messages=messages,
tools=tools,
)
<span class="hljs-built_in">print</span>(final_response.choices[<span class="hljs-number">0</span>].message.content)<!----></pre></div><!----><!----></div><!----> <div hidden=""><p>Truyền kết quả công cụ dưới dạng mục <code>function_call_output</code> trong danh sách <code>input</code> của yêu cầu tiếp theo.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!---->user_message = {<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>, <span class="hljs-string">"content"</span>: <span class="hljs-string">"What's the weather like in San Francisco?"</span>}
response = client.responses.create(
model=<span class="hljs-string">"Qwen/Qwen2.5-7B-Instruct"</span>,
<span class="hljs-built_in">input</span>=[user_message],
tools=tools,
stream=<span class="hljs-literal">False</span>,
)
tool_call = <span class="hljs-built_in">next</span>(item <span class="hljs-keyword">for</span> item <span class="hljs-keyword">in</span> response.output <span class="hljs-keyword">if</span> item.<span class="hljs-built_in">type</span> == <span class="hljs-string">"function_call"</span>)
result = {<span class="hljs-string">"temperature"</span>: <span class="hljs-number">22</span>, <span class="hljs-string">"condition"</span>: <span class="hljs-string">"sunny"</span>}
final_response = client.responses.create(
model=<span class="hljs-string">"Qwen/Qwen2.5-7B-Instruct"</span>,
<span class="hljs-built_in">input</span>=[
user_message,
tool_call.model_dump(exclude_none=<span class="hljs-literal">True</span>),
{<span class="hljs-string">"type"</span>: <span class="hljs-string">"function_call_output"</span>, <span class="hljs-string">"call_id"</span>: tool_call.call_id, <span class="hljs-string">"output"</span>: json.dumps(result)},
],
tools=tools,
stream=<span class="hljs-literal">False</span>,
)
<span class="hljs-built_in">print</span>(final_response.output_text)<!----></pre></div><!----><!----></div><!----><!----></div><!----> <!--[1--><h2 class="relative group"><a id="reasoning" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#reasoning"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Lý luận<!----></span></h2><!--]--><!----> <p>Các mô hình suy luận phát ra một chuỗi suy nghĩ ẩn trước câu trả lời cuối cùng. Máy chủ phát hiện các đoạn suy nghĩ này, loại bỏ các ký hiệu phân tách khỏi câu trả lời hiển thị và hiển thị văn bản suy luận trong một trường riêng biệt để các client có thể render chúng riêng biệt.</p> <ul><li>Chat Completions trả về suy luận dưới dạng <code>reasoning_content</code> trong thông điệp của trợ lý.</li> <li>Responses API trả về suy luận dưới dạng mục đầu ra <code>reasoning</code> đứng trước mục <code>message</code>.</li></ul> <p>Việc phát hiện suy luận dựa vào <a href="../chat_response_parsing">mẫu phản hồi</a>, cho biết vị trí bắt đầu và kết thúc của một đoạn suy nghĩ. Máy chủ sẽ tìm mẫu này ở hai vị trí, theo thứ tự.</p> <ol><li>Thuộc tính <code>response_template</code> của tokenizer, nếu mô hình có kèm theo.</li> <li>Một phương án dự phòng tích hợp, khớp theo kiến trúc mô hình, dành cho các tokenizer chưa được cung cấp kèm theo một template.</li></ol> <p>Các mô hình không có cả hai sẽ được phục vụ mà không có phân tích phản hồi. Văn bản thô đã giải mã được trả về dưới dạng <code>content</code> và không có trường suy luận nào được hiển thị, ngay cả khi mô hình phát ra các thẻ <code><think>...</think></code>.</p> <!--[2--><h3 class="relative group"><a id="enable-reasoning-on-the-server" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#enable-reasoning-on-the-server"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Bật suy luận trên máy chủ<!----></span></h3><!--]--><!----> <p>Sử dụng <code>--reasoning</code> để kiểm soát việc chat template có phát ra các token suy nghĩ hay không.</p> <table><thead><tr><th>Giá trị</th><th>Hành vi</th></tr></thead><tbody><tr><td><code>auto</code> (mặc định)</td><td>Giao cho mặc định của mẫu trò chuyện.</td></tr><tr><td><code>on</code></td><td>Bật ép buộc suy nghĩ bằng cách đặt <code>enable_thinking=True</code> trong kwargs của chat template.</td></tr><tr><td><code>off</code></td><td>Tắt chế độ suy nghĩ bằng cách đặt <code>enable_thinking=False</code>.</td></tr></tbody></table> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-sh "><!---->transformers serve Qwen/Qwen3-1.7B --reasoning on<!----></pre></div><!----> <blockquote class="warning"><p>Không phải tất cả các model đều hỗ trợ reasoning. <code>--reasoning</code> chỉ hoạt động khi chat template của model xử lý biến <code>enable_thinking</code> (hoặc khi tokenizer của nó khai báo các thinking delimiters). Đối với các model khác, cờ này sẽ bị bỏ qua một cách im lặng.</p></blockquote> <p><code>--reasoning on</code> và <code>--reasoning off</code> hoạt động bằng cách đặt biến chat template <code>enable_thinking</code>. Một số chat template sử dụng tên biến khác để bật/tắt chế độ suy nghĩ, hoặc chấp nhận các biến bổ sung làm thay đổi cách prompt được render. Truyền bất kỳ biến chat template nào trực tiếp bằng <code>--chat-template-kwargs</code> dưới dạng một đối tượng JSON.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-sh "><!---->transformers serve Qwen/Qwen3-1.7B \
--chat-template-kwargs <span class="hljs-string">'{"enable_thinking": true}'</span><!----></pre></div><!----> <p>Client cũng có thể gửi <code>chat_template_kwargs</code> trong body của yêu cầu để ghi đè các giá trị mặc định của server cho một yêu cầu duy nhất, mà không cần khởi động lại server.</p> <!--[2--><h3 class="relative group"><a id="chat-completions" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#chat-completions"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Chat Completions<!----></span></h3><!--]--><!----> <p>Phản hồi không dạng luồng bao gồm <code>reasoning_content</code> cùng với <code>content</code> trong thông điệp của trợ lý.</p> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><!--[--><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">openai</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">openai (dòng)</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">curl</div><!--]--></div> <div class="language-select"><div><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> openai <span class="hljs-keyword">import</span> OpenAI
client = OpenAI(base_url=<span class="hljs-string">"http://localhost:8000/v1"</span>, api_key=<span class="hljs-string">"<KEY>"</span>)
completion = client.chat.completions.create(
model=<span class="hljs-string">"Qwen/Qwen3-1.7B"</span>,
messages=[{<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>, <span class="hljs-string">"content"</span>: <span class="hljs-string">"What is 17 * 23?"</span>}],
)
message = completion.choices[<span class="hljs-number">0</span>].message
<span class="hljs-built_in">print</span>(<span class="hljs-string">"reasoning:"</span>, message.reasoning_content)
<span class="hljs-built_in">print</span>(<span class="hljs-string">"answer:"</span>, message.content)<!----></pre></div><!----></div><!----> <div hidden=""><p>Streaming phát ra các delta <code>reasoning_content</code> trước các delta <code>content</code>. Nối từng trường riêng biệt.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!---->chunks = <span class="hljs-built_in">list</span>(
client.chat.completions.create(
model=<span class="hljs-string">"Qwen/Qwen3-1.7B"</span>,
messages=[{<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>, <span class="hljs-string">"content"</span>: <span class="hljs-string">"What is 17 * 23?"</span>}],
stream=<span class="hljs-literal">True</span>,
)
)
reasoning = <span class="hljs-string">""</span>.join(<span class="hljs-built_in">getattr</span>(c.choices[<span class="hljs-number">0</span>].delta, <span class="hljs-string">"reasoning_content"</span>, <span class="hljs-literal">None</span>) <span class="hljs-keyword">or</span> <span class="hljs-string">""</span> <span class="hljs-keyword">for</span> c <span class="hljs-keyword">in</span> chunks)
content = <span class="hljs-string">""</span>.join(c.choices[<span class="hljs-number">0</span>].delta.content <span class="hljs-keyword">or</span> <span class="hljs-string">""</span> <span class="hljs-keyword">for</span> c <span class="hljs-keyword">in</span> chunks)<!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->curl -X POST http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen/Qwen3-1.7B",
"messages": [{"role": "user", "content": "What is 17 * 23?"}],
"chat_template_kwargs": {"enable_thinking": true}
}'<!----></pre></div><!----> <p>Phản hồi bao gồm <code>reasoning_content</code> trong thông điệp của trợ lý.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-json "><!----><span class="hljs-punctuation">{</span>
<span class="hljs-attr">"choices"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
<span class="hljs-punctuation">{</span>
<span class="hljs-attr">"message"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
<span class="hljs-attr">"role"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"assistant"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"content"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"17 * 23 = 391."</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"reasoning_content"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Let me compute 17 * 23. 17 * 20 = 340, 17 * 3 = 51, so 340 + 51 = 391."</span>
<span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"finish_reason"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"stop"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"index"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">]</span>
<span class="hljs-punctuation">}</span><!----></pre></div><!----><!----></div><!----><!----></div><!----> <!--[2--><h3 class="relative group"><a id="responses-api" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#responses-api"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Responses API<!----></span></h3><!--]--><!----> <p>Responses API trả về suy luận dưới dạng một mục đầu ra <code>reasoning</code> riêng biệt, đứng trước mục <code>message</code>.</p> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><!--[--><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">openai</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">openai (dòng)</div><!--]--></div> <div class="language-select"><div><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!---->response = client.responses.create(
model=<span class="hljs-string">"Qwen/Qwen3-1.7B"</span>,
<span class="hljs-built_in">input</span>=<span class="hljs-string">"What is 17 * 23?"</span>,
stream=<span class="hljs-literal">False</span>,
)
reasoning_item = <span class="hljs-built_in">next</span>(i <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> response.output <span class="hljs-keyword">if</span> i.<span class="hljs-built_in">type</span> == <span class="hljs-string">"reasoning"</span>)
message_item = <span class="hljs-built_in">next</span>(i <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> response.output <span class="hljs-keyword">if</span> i.<span class="hljs-built_in">type</span> == <span class="hljs-string">"message"</span>)
<span class="hljs-built_in">print</span>(<span class="hljs-string">"reasoning:"</span>, reasoning_item.content[<span class="hljs-number">0</span>].text)
<span class="hljs-built_in">print</span>(<span class="hljs-string">"answer:"</span>, message_item.content[<span class="hljs-number">0</span>].text)<!----></pre></div><!----></div><!----> <div hidden=""><p>Streaming mở mục suy luận trước và phát các sự kiện <code>response.reasoning_text.delta</code>, sau đó đóng mục đó và mở mục tin nhắn để phát các sự kiện <code>response.output_text.delta</code>.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!---->stream = client.responses.create(
model=<span class="hljs-string">"Qwen/Qwen3-1.7B"</span>,
<span class="hljs-built_in">input</span>=<span class="hljs-string">"What is 17 * 23?"</span>,
stream=<span class="hljs-literal">True</span>,
)
<span class="hljs-keyword">for</span> event <span class="hljs-keyword">in</span> stream:
<span class="hljs-keyword">if</span> event.<span class="hljs-built_in">type</span> == <span class="hljs-string">"response.reasoning_text.delta"</span>:
<span class="hljs-built_in">print</span>(event.delta, end=<span class="hljs-string">""</span>)
<span class="hljs-keyword">elif</span> event.<span class="hljs-built_in">type</span> == <span class="hljs-string">"response.output_text.delta"</span>:
<span class="hljs-built_in">print</span>(event.delta, end=<span class="hljs-string">""</span>)<!----></pre></div><!----><!----></div><!----><!----></div><!----> <!--[2--><h3 class="relative group"><a id="multi-turn-round-trip" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#multi-turn-round-trip"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Đi và về nhiều lượt<!----></span></h3><!--]--><!----> <p>Đối với các cuộc trò chuyện nhiều lượt, hãy bao gồm văn bản suy luận từ phản hồi trước đó trong yêu cầu tiếp theo. Máy chủ sẽ truyền nó cho mẫu trò chuyện, cho phép hiển thị nó như một phần của lượt trợ lý trước đó để mô hình có thể nhìn thấy quá trình suy nghĩ trước đó của chính nó.</p> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><!--[--><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">v1/chat/completions</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">v1/responses</div><!--]--></div> <div class="language-select"><div><p>Gắn <code>reasoning_content</code> vào thông điệp trợ lý trước đó.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!---->first = client.chat.completions.create(
model=<span class="hljs-string">"Qwen/Qwen3-1.7B"</span>,
messages=[{<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>, <span class="hljs-string">"content"</span>: <span class="hljs-string">"What is 17 * 23?"</span>}],
).choices[<span class="hljs-number">0</span>].message
second = client.chat.completions.create(
model=<span class="hljs-string">"Qwen/Qwen3-1.7B"</span>,
messages=[
{<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>, <span class="hljs-string">"content"</span>: <span class="hljs-string">"What is 17 * 23?"</span>},
{
<span class="hljs-string">"role"</span>: <span class="hljs-string">"assistant"</span>,
<span class="hljs-string">"content"</span>: first.content,
<span class="hljs-string">"reasoning_content"</span>: first.reasoning_content,
},
{<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>, <span class="hljs-string">"content"</span>: <span class="hljs-string">"Now multiply that result by 2."</span>},
],
)
<span class="hljs-built_in">print</span>(second.choices[<span class="hljs-number">0</span>].message.content)<!----></pre></div><!----><!----></div><!----> <div hidden=""><p>Truyền mục <code>reasoning</code> trước đó lại như một mục đầu vào trước thông điệp của trợ lý.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!---->first = client.responses.create(
model=<span class="hljs-string">"Qwen/Qwen3-1.7B"</span>,
<span class="hljs-built_in">input</span>=<span class="hljs-string">"What is 17 * 23?"</span>,
stream=<span class="hljs-literal">False</span>,
)
reasoning_item = <span class="hljs-built_in">next</span>(i <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> first.output <span class="hljs-keyword">if</span> i.<span class="hljs-built_in">type</span> == <span class="hljs-string">"reasoning"</span>)
message_item = <span class="hljs-built_in">next</span>(i <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> first.output <span class="hljs-keyword">if</span> i.<span class="hljs-built_in">type</span> == <span class="hljs-string">"message"</span>)
second = client.responses.create(
model=<span class="hljs-string">"Qwen/Qwen3-1.7B"</span>,
<span class="hljs-built_in">input</span>=[
{<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>, <span class="hljs-string">"content"</span>: <span class="hljs-string">"What is 17 * 23?"</span>},
reasoning_item.model_dump(exclude_none=<span class="hljs-literal">True</span>),
{<span class="hljs-string">"role"</span>: <span class="hljs-string">"assistant"</span>, <span class="hljs-string">"content"</span>: message_item.content[<span class="hljs-number">0</span>].text},
{<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>, <span class="hljs-string">"content"</span>: <span class="hljs-string">"Now multiply that result by 2."</span>},
],
stream=<span class="hljs-literal">False</span>,
)
<span class="hljs-built_in">print</span>(second.output_text)<!----></pre></div><!----><!----></div><!----><!----></div><!----> <!--[1--><h2 class="relative group"><a id="port-forwarding" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#port-forwarding"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Chuyển tiếp cổng<!----></span></h2><!--]--><!----> <p>Port forwarding cho phép bạn cung cấp các mô hình từ một máy chủ từ xa. Hãy đảm bảo bạn có quyền truy cập SSH vào máy chủ, sau đó chạy lệnh này trên máy cục bộ của bạn.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-bash "><!---->ssh -N -f -L 8000:localhost:8000 your_server_account@your_server_IP -p port_to_ssh_into_your_server<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="reproducibility" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#reproducibility"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Tính khả tái lập<!----></span></h2><!--]--><!----> <p>Truyền model như một đối số vị trí để tránh các gợi ý model cho từng yêu cầu và tạo ra các lần chạy ổn định, có thể lặp lại.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-sh "><!---->transformers serve Qwen/Qwen2.5-0.5B-Instruct \
--continuous-batching \
--dtype <span class="hljs-string">"bfloat16"</span><!----></pre></div><!----> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/transformers/blob/main/docs/source/en/serve-cli/serving.md" target="_blank"><svg class="mr-1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M31,16l-7,7l-1.41-1.41L28.17,16l-5.58-5.59L24,9l7,7z"></path><path d="M1,16l7-7l1.41,1.41L3.83,16l5.58,5.59L8,23l-7-7z"></path><path d="M12.419,25.484L17.639,6.552l1.932,0.518L14.351,26.002z"></path></svg><!----> <span><span class="underline">Update</span> on GitHub</span></a><!----> <p></p><!--]--><!----><!--]--><!--]--><!--]--> <!--[-1--><!--]--><!--]-->
<script>
{
__sveltekit_p1l6wl = {
base: "/docs/transformers/main/en",
assets: "/docs/transformers/main/en"
};
const element = document.currentScript.parentElement;
Promise.all([
import("/docs/transformers/main/en/_app/immutable/entry/start.DS27pc2p.js"),
import("/docs/transformers/main/en/_app/immutable/entry/app.JevHRe6k.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 604],
data: [null,null],
form: null,
error: null
});
});
}
</script>
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"Serve CLI","local":"serve-cli","sections":[{"title":"v1/chat/completions","local":"v1chatcompletions","sections":[{"title":"Hoàn thành dựa trên văn bản","local":"text-based-completions","sections":[],"depth":3},{"title":"Hoàn thành dựa trên hình ảnh","local":"image-based-completions","sections":[],"depth":3},{"title":"Hoàn thành dựa trên âm thanh","local":"audio-based-completions","sections":[],"depth":3},{"title":"Hoàn thành dựa trên video","local":"video-based-completions","sections":[],"depth":3},{"title":"Cuộc trò chuyện nhiều lượt","local":"completions","sections":[],"depth":3}],"depth":2},{"title":"v1/completions","local":"v1completions","sections":[],"depth":2},{"title":"v1/responses","local":"v1responses","sections":[{"title":"Phản hồi dựa trên văn bản","local":"text-based-responses","sections":[],"depth":3},{"title":"Phản hồi dựa trên hình ảnh","local":"image-based-responses","sections":[],"depth":3},{"title":"Phản hồi dựa trên âm thanh","local":"audio-based-responses","sections":[],"depth":3},{"title":"Phản hồi dựa trên video","local":"video-based-responses","sections":[],"depth":3},{"title":"Cuộc trò chuyện nhiều lượt","local":"responses","sections":[],"depth":3}],"depth":2},{"title":"v1/systemone","local":"v1systemone","sections":[{"title":"Cấu hình quyết định","local":"decision-configuration","sections":[],"depth":3}],"depth":2},{"title":"v1/audio/transcriptions","local":"v1audiotranscriptions","sections":[],"depth":2},{"title":"v1/models","local":"v1models","sections":[],"depth":2},{"title":"Tải mô hình","local":"loading-models","sections":[{"title":"Yêu cầu","local":"request","sections":[],"depth":3},{"title":"Phản hồi","local":"response","sections":[],"depth":3},{"title":"Các giai đoạn","local":"stages","sections":[],"depth":3}],"depth":2},{"title":"Timeout","local":"timeout","sections":[{"title":"Đang tải các ví dụ","local":"loading-examples","sections":[],"depth":3}],"depth":2},{"title":"Gọi công cụ","local":"tool-calling","sections":[{"title":"Gọi công cụ đa lượt","local":"multi-turn-tool-calling","sections":[],"depth":3}],"depth":2},{"title":"Lý luận","local":"reasoning","sections":[{"title":"Bật suy luận trên máy chủ","local":"enable-reasoning-on-the-server","sections":[],"depth":3},{"title":"Chat Completions","local":"chat-completions","sections":[],"depth":3},{"title":"Responses API","local":"responses-api","sections":[],"depth":3},{"title":"Đi và về nhiều lượt","local":"multi-turn-round-trip","sections":[],"depth":3}],"depth":2},{"title":"Chuyển tiếp cổng","local":"port-forwarding","sections":[],"depth":2},{"title":"Tính khả tái lập","local":"reproducibility","sections":[],"depth":2}],"depth":1}"/>
<link href="/docs/transformers/main/en/_app/immutable/entry/start.DS27pc2p.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/Bbt6a5UJ.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DGxd7hEC.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/entry/app.JevHRe6k.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/hD794eHD.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DG_DvbXP.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DOLBYqcr.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DsnmJJEf.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/nodes/0.Dperbprz.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DIDdACb8.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/nodes/604.Dwc2d6Ao.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/CBFQlbiL.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/BUd-buby.js" rel="modulepreload">
<!--1rphhhg--><meta name="hf:doc:metadata" content="{"title":"Serve CLI","local":"serve-cli","sections":[{"title":"v1/chat/completions","local":"v1chatcompletions","sections":[{"title":"Text-based completions","local":"text-based-completions","sections":[],"depth":3},{"title":"Image-based completions","local":"image-based-completions","sections":[],"depth":3},{"title":"Audio-based completions","local":"audio-based-completions","sections":[],"depth":3},{"title":"Video-based completions","local":"video-based-completions","sections":[],"depth":3},{"title":"Multi-turn conversations","local":"completions","sections":[],"depth":3}],"depth":2},{"title":"v1/completions","local":"v1completions","sections":[],"depth":2},{"title":"v1/responses","local":"v1responses","sections":[{"title":"Text-based responses","local":"text-based-responses","sections":[],"depth":3},{"title":"Image-based responses","local":"image-based-responses","sections":[],"depth":3},{"title":"Audio-based responses","local":"audio-based-responses","sections":[],"depth":3},{"title":"Video-based responses","local":"video-based-responses","sections":[],"depth":3},{"title":"Multi-turn conversations","local":"responses","sections":[],"depth":3}],"depth":2},{"title":"v1/systemone","local":"v1systemone","sections":[{"title":"Decision configuration","local":"decision-configuration","sections":[],"depth":3}],"depth":2},{"title":"v1/audio/transcriptions","local":"v1audiotranscriptions","sections":[],"depth":2},{"title":"v1/models","local":"v1models","sections":[],"depth":2},{"title":"Loading models","local":"loading-models","sections":[{"title":"Request","local":"request","sections":[],"depth":3},{"title":"Response","local":"response","sections":[],"depth":3},{"title":"Stages","local":"stages","sections":[],"depth":3}],"depth":2},{"title":"Timeout","local":"timeout","sections":[{"title":"Loading examples","local":"loading-examples","sections":[],"depth":3}],"depth":2},{"title":"Tool calling","local":"tool-calling","sections":[{"title":"Multi-turn tool calling","local":"multi-turn-tool-calling","sections":[],"depth":3}],"depth":2},{"title":"Reasoning","local":"reasoning","sections":[{"title":"Enable reasoning on the server","local":"enable-reasoning-on-the-server","sections":[],"depth":3},{"title":"Chat Completions","local":"chat-completions","sections":[],"depth":3},{"title":"Responses API","local":"responses-api","sections":[],"depth":3},{"title":"Multi-turn round trip","local":"multi-turn-round-trip","sections":[],"depth":3}],"depth":2},{"title":"Port forwarding","local":"port-forwarding","sections":[],"depth":2},{"title":"Reproducibility","local":"reproducibility","sections":[],"depth":2}],"depth":1}"/><!---->
<link href="/docs/transformers/main/en/_app/immutable/assets/0.tn0RQdqM.css" rel="modulepreload"> <!--[--><!--[0--><!--[--><!--[0--><!--[--><p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: inline-end; margin-inline-start: 10px; display: inline-flex; position: relative; z-index: 10;"><div dir="ltr" class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----></span> <span dir="auto">Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg><!----></button></div> <!--[-1--><!--]--></div><!----> <!--[0--><h1 class="relative group"><a id="serve-cli" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#serve-cli"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Serve CLI<!----></span></h1><!--]--><!----> <p>CLI <code>transformers serve</code> là một lựa chọn nhẹ nhàng cho các máy chủ cục bộ hoặc tự lưu trữ. Nó tránh được chi phí thời gian chạy và vận hành bổ sung của các bộ máy suy luận chuyên dụng như vLLM. Hãy sử dụng nó cho việc đánh giá, thử nghiệm và triển khai với tải vừa phải. Các tính năng như <a href="../continuous_batching">continuous batching</a> giúp tăng thông lượng và giảm độ trễ.</p> <blockquote class="tip"><p>Đối với triển khai sản xuất quy mô lớn, hãy sử dụng vLLM hoặc SGLang với một mô hình Transformer làm backend. Tìm hiểu thêm trong hướng dẫn <a href="../community_integrations/transformers_as_backend">Các backend suy luận</a>.</p></blockquote> <p>Lệnh <code>transformers serve</code> khởi tạo một máy chủ cục bộ tương thích với <a href="https://platform.openai.com/docs/overview" rel="nofollow">OpenAI SDK</a>. Máy chủ này hoạt động với nhiều ứng dụng bên thứ ba và hỗ trợ các API REST bên dưới.</p> <ul><li><code>/v1/chat/completions</code> cho các yêu cầu văn bản, hình ảnh, âm thanh và video</li> <li><code>/v1/completions</code> cho các hoàn thành văn bản cũ từ một prompt tự do</li> <li><code>/v1/responses</code> hỗ trợ <a href="https://platform.openai.com/docs/api-reference/responses" rel="nofollow">Responses API</a></li> <li><code>/v1/audio/transcriptions</code> cho việc chuyển đổi âm thanh thành văn bản</li> <li><code>/v1/systemone</code> trả lời các câu hỏi có dạng (lựa chọn, điểm số, có/không) với một xác suất cho mỗi câu trả lời được phép</li> <li><code>/v1/models</code> liệt kê các model khả dụng cho các tích hợp bên thứ ba</li> <li><code>/load_model</code> truyền tiến trình tải mô hình qua SSE</li></ul> <p>Cài đặt các phụ thuộc phục vụ.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-bash "><!---->pip install transformers[serving]<!----></pre></div><!----> <p>Chạy <code>transformers serve</code> để khởi động máy chủ. Địa chỉ máy chủ mặc định là http://localhost:8000.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->transformers serve<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="v1chatcompletions" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#v1chatcompletions"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->v1/chat/completions<!----></span></h2><!--]--><!----> <p>API <code>v1/chat/completions</code> được xây dựng dựa trên <a href="https://platform.openai.com/docs/api-reference/chat" rel="nofollow">Chat Completions API</a>. Nó hỗ trợ các yêu cầu văn bản, hình ảnh, âm thanh và video cho LLM, VLM và các mô hình đa phương thức. Hãy sử dụng nó với <code>curl</code>, <a href="https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient" rel="nofollow">InferenceClient</a> hoặc client <a href="https://platform.openai.com/docs/quickstart" rel="nofollow">OpenAI</a>.</p> <!--[2--><h3 class="relative group"><a id="text-based-completions" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#text-based-completions"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Hoàn thành dựa trên văn bản<!----></span></h3><!--]--><!----> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><!--[--><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">huggingface_hub</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">huggingface_hub (dòng)</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">openai</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">openai (dòng)</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">curl</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">curl (dòng)</div><!--]--></div> <div class="language-select"><div><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> InferenceClient
messages = [{<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>, <span class="hljs-string">"content"</span>: <span class="hljs-string">"What is the Transformers library known for?"</span>}]
client = InferenceClient(<span class="hljs-string">"http://localhost:8000"</span>)
result = client.chat_completion(messages, model=<span class="hljs-string">"Qwen/Qwen2.5-0.5B-Instruct"</span>, max_tokens=<span class="hljs-number">256</span>)
<span class="hljs-built_in">print</span>(result.choices[<span class="hljs-number">0</span>].message.content)<!----></pre></div><!----> <p><a href="https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient" rel="nofollow">InferenceClient</a> trả về một chuỗi đã được in ra.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->The Transformers library is primarily known for its ability to create and manipulate large-scale language models [...]<!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> InferenceClient
messages = [{<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>, <span class="hljs-string">"content"</span>: <span class="hljs-string">"What is the Transformers library known for?"</span>}]
client = InferenceClient(<span class="hljs-string">"http://localhost:8000"</span>)
stream = client.chat_completion(messages, model=<span class="hljs-string">"Qwen/Qwen2.5-0.5B-Instruct"</span>, max_tokens=<span class="hljs-number">256</span>, stream=<span class="hljs-literal">True</span>)
<span class="hljs-keyword">for</span> chunk <span class="hljs-keyword">in</span> stream:
token = chunk.choices[<span class="hljs-number">0</span>].delta.content
<span class="hljs-keyword">if</span> token:
<span class="hljs-built_in">print</span>(token, end=<span class="hljs-string">""</span>)<!----></pre></div><!----> <p><a href="https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient" rel="nofollow">InferenceClient</a> trả về một chuỗi đã được in ra.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->The Transformers library is primarily known for its ability to create and manipulate large-scale language models [...]<!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> openai <span class="hljs-keyword">import</span> OpenAI
client = OpenAI(base_url=<span class="hljs-string">"http://localhost:8000/v1"</span>, api_key=<span class="hljs-string">"<random_string>"</span>)
completion = client.chat.completions.create(
model=<span class="hljs-string">"Qwen/Qwen2.5-0.5B-Instruct"</span>,
messages=[
{
<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>,
<span class="hljs-string">"content"</span>: <span class="hljs-string">"What is the Transformers library known for?"</span>
}
],
)
<span class="hljs-built_in">print</span>(completion.choices[<span class="hljs-number">0</span>].message.content)<!----></pre></div><!----> <p>Client <a href="https://platform.openai.com/docs/quickstart" rel="nofollow">OpenAI</a> trả về một chuỗi đã được in ra.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->The Transformers library is primarily known for its ability to create and manipulate large-scale language models [...]<!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> openai <span class="hljs-keyword">import</span> OpenAI
client = OpenAI(base_url=<span class="hljs-string">"http://localhost:8000/v1"</span>, api_key=<span class="hljs-string">"<random_string>"</span>)
completion = client.chat.completions.create(
model=<span class="hljs-string">"Qwen/Qwen2.5-0.5B-Instruct"</span>,
messages=[
{
<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>,
<span class="hljs-string">"content"</span>: <span class="hljs-string">"What is the Transformers library known for?"</span>
}
],
stream=<span class="hljs-literal">True</span>,
)
<span class="hljs-keyword">for</span> chunk <span class="hljs-keyword">in</span> completion:
token = chunk.choices[<span class="hljs-number">0</span>].delta.content
<span class="hljs-keyword">if</span> token:
<span class="hljs-built_in">print</span>(token, end=<span class="hljs-string">""</span>)<!----></pre></div><!----> <p>Client <a href="https://platform.openai.com/docs/quickstart" rel="nofollow">OpenAI</a> trả về một chuỗi đã được in ra.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->The Transformers library is primarily known for its ability to create and manipulate large-scale language models [...]<!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->curl -X POST http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen/Qwen2.5-0.5B-Instruct",
"messages": [{"role": "user", "content": "What is the Transformers library known for?"}],
"temperature": 0.9,
"max_tokens": 256
}'<!----></pre></div><!----> <p>Lệnh trả về một chuỗi JSON.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-json "><!----><span class="hljs-punctuation">{</span>
<span class="hljs-attr">"id"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"ade3578d-e2c3-47ed-b651-c014991a92f6"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"choices"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
<span class="hljs-punctuation">{</span>
<span class="hljs-attr">"finish_reason"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"length"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"index"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"message"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
<span class="hljs-attr">"content"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"The Transformers library is primarily known for its ability to create and manipulate large-scale language models [...]"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"role"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"assistant"</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"created"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1776184004</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"model"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Qwen/Qwen2.5-0.5B-Instruct@main"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"object"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"chat.completion"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"usage"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
<span class="hljs-attr">"completion_tokens"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">256</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"prompt_tokens"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">37</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"total_tokens"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">293</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span><!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->curl -X POST http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen/Qwen2.5-0.5B-Instruct",
"messages": [{"role": "user", "content": "What is the Transformers library known for?"}],
"temperature": 0.9,
"max_tokens": 256,
"stream": true
}'<!----></pre></div><!----> <p>Lệnh trả về một luồng các khối dữ liệu.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->data: {"id":"596d3941-754d-43b4-8773-9bf9940049f5","choices":[{"delta":{"role":"assistant"},"index":0}],"created":1776183469,"model":"Qwen/Qwen2.5-0.5B-Instruct@main","object":"chat.completion.chunk","system_fingerprint":""}
data: {"id":"596d3941-754d-43b4-8773-9bf9940049f5","choices":[{"delta":{"content":"The"},"index":0}],"created":1776183469,"model":"Qwen/Qwen2.5-0.5B-Instruct@main","object":"chat.completion.chunk","system_fingerprint":""}
Xet Storage Details
- Size:
- 405 kB
- Xet hash:
- 042634df4b1a2910543634b9940d9edc03d9e979613b105d1aa3fe53cfeb8a08
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.