import{s as Ct,n as Wt,o as Gt}from"../chunks/scheduler.f9cad8c1.js";import{S as Vt,i as Zt,e as d,s,c as i,h as _t,a as u,d as l,b as a,f as It,g as p,j as R,k as Bt,l as vt,m as n,n as r,t as o,o as m,p as $}from"../chunks/index.6e06ef99.js";import{C as xt,H as M,E as Et}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.c4302a62.js";import{C as H}from"../chunks/CodeBlock.72594161.js";function Nt(ht){let f,Q,A,X,y,z,c,Y,w,gt=`이 가이드는 smolagents 라이브러리의 동기 에이전트를 Starlette 기반의 비동기 Python 웹 애플리케이션에 통합하는 방법을 설명합니다.
비동기 Python과 에이전트 통합을 처음 접하는 사용자들이 동기 에이전트 로직과 비동기 웹 서버를 효과적으로 결합하는 모범 사례를 익힐 수 있도록 구성했습니다.`,F,h,L,g,Tt="<li><strong>Starlette</strong>: Python에서 비동기 웹 애플리케이션을 구축하기 위한 경량 ASGI 프레임워크입니다.</li> <li><strong>anyio.to_thread.run_sync</strong>: 블로킹(동기) 코드를 백그라운드 스레드에서 실행하여 비동기 이벤트 루프를 차단하지 않도록 하는 유틸리티입니다.</li> <li><strong>CodeAgent</strong>: 프로그래밍 방식으로 작업을 해결할 수 있는 <code>smolagents</code> 라이브러리의 에이전트입니다.</li>",P,T,O,J,Jt="<code>CodeAgent.run()</code>은 Python 코드를 동기적으로 실행합니다. 비동기 엔드포인트에서 직접 호출하면 Starlette의 이벤트 루프를 차단하여 성능과 확장성이 저하됩니다. <code>anyio.to_thread.run_sync</code>로 이 작업을 백그라운드 스레드로 위임하면 높은 동시성에서도 앱의 응답성과 효율성을 유지할 수 있습니다.",D,j,K,b,jt="<li>Starlette 앱은 <code>task</code> 문자열이 포함된 JSON 페이로드를 받는 <code>/run-agent</code> 엔드포인트를 노출합니다.</li> <li>요청이 수신되면 <code>anyio.to_thread.run_sync</code>를 사용하여 백그라운드 스레드에서 에이전트가 실행됩니다.</li> <li>결과는 JSON 응답으로 반환됩니다.</li>",tt,U,et,k,lt,I,nt,B,st,C,at,W,it,G,pt,V,rt,Z,ot,_,bt="<strong>예상 응답:</strong>",mt,v,$t,x,dt,E,Ut='<li><a href="https://www.starlette.io/" rel="nofollow">Starlette 문서</a></li> <li><a href="https://anyio.readthedocs.io/" rel="nofollow">anyio 문서</a></li>',ut,Mt,ft,N,kt='전체 코드는 <a href="https://github.com/huggingface/smolagents/tree/main/examples/async_agent" rel="nofollow"><code>examples/async_agent</code></a>를 참고하세요.',yt,S,ct,q,wt;return y=new xt({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),c=new M({props:{title:"에이전트를 활용한 비동기 애플리케이션",local:"async-applications-with-agents",headingTag:"h1"}}),h=new M({props:{title:"개요",local:"overview",headingTag:"h2"}}),T=new M({props:{title:"백그라운드 스레드를 사용하는 이유는?",local:"why-use-a-background-thread?",headingTag:"h2"}}),j=new M({props:{title:"예시 워크플로우",local:"example-workflow",headingTag:"h2"}}),U=new M({props:{title:"CodeAgent를 활용한 Starlette 앱 구축",local:"building-a-starlette-app-with-a-codeagent",headingTag:"h2"}}),k=new M({props:{title:"1. 의존성 설치",local:"1.-install-dependencies",headingTag:"h3"}}),I=new H({props:{code:"cGlwJTIwaW5zdGFsbCUyMHNtb2xhZ2VudHMlMjBzdGFybGV0dGUlMjBhbnlpbyUyMHV2aWNvcm4=",highlighted:"pip install smolagents starlette anyio uvicorn",wrap:!1}}),B=new M({props:{title:"2. 애플리케이션 코드 ( main.py )",local:"2.-application-code-( main.py )",headingTag:"h3"}}),C=new H({props:{code:"aW1wb3J0JTIwYW55aW8udG9fdGhyZWFkJTBBZnJvbSUyMHN0YXJsZXR0ZS5hcHBsaWNhdGlvbnMlMjBpbXBvcnQlMjBTdGFybGV0dGUlMEFmcm9tJTIwc3RhcmxldHRlLnJlcXVlc3RzJTIwaW1wb3J0JTIwUmVxdWVzdCUwQWZyb20lMjBzdGFybGV0dGUucmVzcG9uc2VzJTIwaW1wb3J0JTIwSlNPTlJ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NEJTVCJTIyUE9TVCUyMiU1RCklMkMlMEElNUQp",highlighted:`<span class="hljs-keyword">import</span> anyio.to_thread
<span class="hljs-keyword">from</span> starlette.applications <span class="hljs-keyword">import</span> Starlette
<span class="hljs-keyword">from</span> starlette.requests <span class="hljs-keyword">import</span> Request
<span class="hljs-keyword">from</span> starlette.responses <span class="hljs-keyword">import</span> JSONResponse
<span class="hljs-keyword">from</span> starlette.routing <span class="hljs-keyword">import</span> Route

<span class="hljs-keyword">from</span> smolagents <span class="hljs-keyword">import</span> CodeAgent, InferenceClientModel

agent = CodeAgent(
    model=InferenceClientModel(model_id=<span class="hljs-string">&quot;Qwen/Qwen3-Next-80B-A3B-Thinking&quot;</span>),
    tools=[],
)

<span class="hljs-keyword">async</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">run_agent</span>(<span class="hljs-params">request: Request</span>):
    data = <span class="hljs-keyword">await</span> request.json()
    task = data.get(<span class="hljs-string">&quot;task&quot;</span>, <span class="hljs-string">&quot;&quot;</span>)
    <span class="hljs-comment"># Run the agent synchronously in a background thread</span>
    result = <span class="hljs-keyword">await</span> anyio.to_thread.run_sync(agent.run, task)
    <span class="hljs-keyword">return</span> JSONResponse({<span class="hljs-string">&quot;result&quot;</span>: result})

app = Starlette(routes=[
    Route(<span class="hljs-string">&quot;/run-agent&quot;</span>, run_agent, methods=[<span class="hljs-string">&quot;POST&quot;</span>]),
])`,wrap:!1}}),W=new M({props:{title:"3. 앱 실행",local:"3.-run-the-app",headingTag:"h3"}}),G=new H({props:{code:"dXZpY29ybiUyMGFzeW5jX2FnZW50Lm1haW4lM0FhcHAlMjAtLXJlbG9hZA==",highlighted:"uvicorn async_agent.main:app --reload",wrap:!1}}),V=new M({props:{title:"4. 엔드포인트 테스트",local:"4.-test-the-endpoint",headingTag:"h3"}}),Z=new H({props:{code:"Y3VybCUyMC1YJTIwUE9TVCUyMGh0dHAlM0ElMkYlMkZsb2NhbGhvc3QlM0E4MDAwJTJGcnVuLWFnZW50JTIwLUglMjAnQ29udGVudC1UeXBlJTNBJTIwYXBwbGljYXRpb24lMkZqc29uJyUyMC1kJTIwJyU3QiUyMnRhc2slMjIlM0ElMjAlMjJXaGF0JTIwaXMlMjAyJTJCMiUzRiUyMiU3RCc=",highlighted:'curl -X POST http://localhost:8000/run-agent -H <span class="hljs-string">&#x27;Content-Type: application/json&#x27;</span> -d <span class="hljs-string">&#x27;{&quot;task&quot;: &quot;What is 2+2?&quot;}&#x27;</span>',wrap:!1}}),v=new H({props:{code:"JTdCJTIycmVzdWx0JTIyJTNBJTIwJTIyNCUyMiU3RA==",highlighted:'<span class="hljs-punctuation">{</span><span class="hljs-attr">&quot;result&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;4&quot;</span><span class="hljs-punctuation">}</span>',wrap:!1}}),x=new M({props:{title:"추가 자료",local:"further-reading",headingTag:"h2"}}),S=new Et({props:{source:"https://github.com/huggingface/smolagents/blob/main/docs/source/ko/examples/async_agent.md"}}),{c(){f=d("meta"),Q=s(),A=d("p"),X=s(),i(y.$$.fragment),z=s(),i(c.$$.fragment),Y=s(),w=d("p"),w.textContent=gt,F=s(),i(h.$$.fragment),L=s(),g=d("ul"),g.innerHTML=Tt,P=s(),i(T.$$.fragment),O=s(),J=d("p"),J.innerHTML=Jt,D=s(),i(j.$$.fragment),K=s(),b=d("ul"),b.innerHTML=jt,tt=s(),i(U.$$.fragment),et=s(),i(k.$$.fragment),lt=s(),i(I.$$.fragment),nt=s(),i(B.$$.fragment),st=s(),i(C.$$.fragment),at=s(),i(W.$$.fragment),it=s(),i(G.$$.fragment),pt=s(),i(V.$$.fragment),rt=s(),i(Z.$$.fragment),ot=s(),_=d("p"),_.innerHTML=bt,mt=s(),i(v.$$.fragment),$t=s(),i(x.$$.fragment),dt=s(),E=d("ul"),E.innerHTML=Ut,ut=s(),Mt=d("hr"),ft=s(),N=d("p"),N.innerHTML=kt,yt=s(),i(S.$$.fragment),ct=s(),q=d("p"),this.h()},l(t){const e=_t("svelte-u9bgzb",document.head);f=u(e,"META",{name:!0,content:!0}),e.forEach(l),Q=a(t),A=u(t,"P",{}),It(A).forEach(l),X=a(t),p(y.$$.fragment,t),z=a(t),p(c.$$.fragment,t),Y=a(t),w=u(t,"P",{"data-svelte-h":!0}),R(w)!=="svelte-a7vyi1"&&(w.textContent=gt),F=a(t),p(h.$$.fragment,t),L=a(t),g=u(t,"UL",{"data-svelte-h":!0}),R(g)!=="svelte-1m3l35r"&&(g.innerHTML=Tt),P=a(t),p(T.$$.fragment,t),O=a(t),J=u(t,"P",{"data-svelte-h":!0}),R(J)!=="svelte-4ys3p"&&(J.innerHTML=Jt),D=a(t),p(j.$$.fragment,t),K=a(t),b=u(t,"UL",{"data-svelte-h":!0}),R(b)!=="svelte-rsi6kb"&&(b.innerHTML=jt),tt=a(t),p(U.$$.fragment,t),et=a(t),p(k.$$.fragment,t),lt=a(t),p(I.$$.fragment,t),nt=a(t),p(B.$$.fragment,t),st=a(t),p(C.$$.fragment,t),at=a(t),p(W.$$.fragment,t),it=a(t),p(G.$$.fragment,t),pt=a(t),p(V.$$.fragment,t),rt=a(t),p(Z.$$.fragment,t),ot=a(t),_=u(t,"P",{"data-svelte-h":!0}),R(_)!=="svelte-jyqe30"&&(_.innerHTML=bt),mt=a(t),p(v.$$.fragment,t),$t=a(t),p(x.$$.fragment,t),dt=a(t),E=u(t,"UL",{"data-svelte-h":!0}),R(E)!=="svelte-4ks0ah"&&(E.innerHTML=Ut),ut=a(t),Mt=u(t,"HR",{}),ft=a(t),N=u(t,"P",{"data-svelte-h":!0}),R(N)!=="svelte-1nhw2v8"&&(N.innerHTML=kt),yt=a(t),p(S.$$.fragment,t),ct=a(t),q=u(t,"P",{}),It(q).forEach(l),this.h()},h(){Bt(f,"name","hf:doc:metadata"),Bt(f,"content",St)},m(t,e){vt(document.head,f),n(t,Q,e),n(t,A,e),n(t,X,e),r(y,t,e),n(t,z,e),r(c,t,e),n(t,Y,e),n(t,w,e),n(t,F,e),r(h,t,e),n(t,L,e),n(t,g,e),n(t,P,e),r(T,t,e),n(t,O,e),n(t,J,e),n(t,D,e),r(j,t,e),n(t,K,e),n(t,b,e),n(t,tt,e),r(U,t,e),n(t,et,e),r(k,t,e),n(t,lt,e),r(I,t,e),n(t,nt,e),r(B,t,e),n(t,st,e),r(C,t,e),n(t,at,e),r(W,t,e),n(t,it,e),r(G,t,e),n(t,pt,e),r(V,t,e),n(t,rt,e),r(Z,t,e),n(t,ot,e),n(t,_,e),n(t,mt,e),r(v,t,e),n(t,$t,e),r(x,t,e),n(t,dt,e),n(t,E,e),n(t,ut,e),n(t,Mt,e),n(t,ft,e),n(t,N,e),n(t,yt,e),r(S,t,e),n(t,ct,e),n(t,q,e),wt=!0},p:Wt,i(t){wt||(o(y.$$.fragment,t),o(c.$$.fragment,t),o(h.$$.fragment,t),o(T.$$.fragment,t),o(j.$$.fragment,t),o(U.$$.fragment,t),o(k.$$.fragment,t),o(I.$$.fragment,t),o(B.$$.fragment,t),o(C.$$.fragment,t),o(W.$$.fragment,t),o(G.$$.fragment,t),o(V.$$.fragment,t),o(Z.$$.fragment,t),o(v.$$.fragment,t),o(x.$$.fragment,t),o(S.$$.fragment,t),wt=!0)},o(t){m(y.$$.fragment,t),m(c.$$.fragment,t),m(h.$$.fragment,t),m(T.$$.fragment,t),m(j.$$.fragment,t),m(U.$$.fragment,t),m(k.$$.fragment,t),m(I.$$.fragment,t),m(B.$$.fragment,t),m(C.$$.fragment,t),m(W.$$.fragment,t),m(G.$$.fragment,t),m(V.$$.fragment,t),m(Z.$$.fragment,t),m(v.$$.fragment,t),m(x.$$.fragment,t),m(S.$$.fragment,t),wt=!1},d(t){t&&(l(Q),l(A),l(X),l(z),l(Y),l(w),l(F),l(L),l(g),l(P),l(O),l(J),l(D),l(K),l(b),l(tt),l(et),l(lt),l(nt),l(st),l(at),l(it),l(pt),l(rt),l(ot),l(_),l(mt),l($t),l(dt),l(E),l(ut),l(Mt),l(ft),l(N),l(yt),l(ct),l(q)),l(f),$(y,t),$(c,t),$(h,t),$(T,t),$(j,t),$(U,t),$(k,t),$(I,t),$(B,t),$(C,t),$(W,t),$(G,t),$(V,t),$(Z,t),$(v,t),$(x,t),$(S,t)}}}const St='{"title":"에이전트를 활용한 비동기 애플리케이션","local":"async-applications-with-agents","sections":[{"title":"개요","local":"overview","sections":[],"depth":2},{"title":"백그라운드 스레드를 사용하는 이유는?","local":"why-use-a-background-thread?","sections":[],"depth":2},{"title":"예시 워크플로우","local":"example-workflow","sections":[],"depth":2},{"title":"CodeAgent를 활용한 Starlette 앱 구축","local":"building-a-starlette-app-with-a-codeagent","sections":[{"title":"1. 의존성 설치","local":"1.-install-dependencies","sections":[],"depth":3},{"title":"2. 애플리케이션 코드 ( main.py )","local":"2.-application-code-( main.py )","sections":[],"depth":3},{"title":"3. 앱 실행","local":"3.-run-the-app","sections":[],"depth":3},{"title":"4. 엔드포인트 테스트","local":"4.-test-the-endpoint","sections":[],"depth":3}],"depth":2},{"title":"추가 자료","local":"further-reading","sections":[],"depth":2}],"depth":1}';function Rt(ht){return Gt(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Xt extends Vt{constructor(f){super(),Zt(this,f,Rt,Nt,Ct,{})}}export{Xt as component};
