1"use strict";(globalThis.webpackChunkwebsite=globalThis.webpackChunkwebsite||[]).push([[8278],{9400(e,n,t){t.r(n),t.d(n,{assets:()=>l,contentTitle:()=>i,default:()=>h,frontMatter:()=>r,metadata:()=>s,toc:()=>c});const s=JSON.parse('{"id":"examples/python","title":"Python","description":"---","source":"@site/../docs/examples/python.md","sourceDirName":"examples","slug":"/examples/python","permalink":"/next/examples/python","draft":false,"unlisted":false,"editUrl":"https://github.com/Inebrio/Routerly/edit/main/../docs/examples/python.md","tags":[],"version":"current","frontMatter":{"title":"Python","sidebar_label":"Python"},"sidebar":"docsSidebar","previous":{"title":"JavaScript / TypeScript","permalink":"/next/examples/javascript"},"next":{"title":"Java","permalink":"/next/examples/java"}}');var a=t(4848),o=t(8453);const r={title:"Python",sidebar_label:"Python"},i="Python",l={},c=[{value:"OpenAI SDK",id:"openai-sdk",level:2},{value:"Anthropic SDK",id:"anthropic-sdk",level:2},{value:"Raw HTTP (httpx)",id:"raw-http-httpx",level:2}];function p(e){const n={admonition:"admonition",code:"code",h1:"h1",h2:"h2",header:"header",hr:"hr",p:"p",pre:"pre",...(0,o.R)(),...e.components};return(0,a.jsxs)(a.Fragment,{children:[(0,a.jsx)(n.header,{children:(0,a.jsx)(n.h1,{id:"python",children:"Python"})}),"\n",(0,a.jsx)(n.hr,{}),"\n",(0,a.jsx)(n.h2,{id:"openai-sdk",children:"OpenAI SDK"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-bash",children:"pip install openai\n"})}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-python",children:'from openai import OpenAI\n\nclient = OpenAI(\n base_url="http://localhost:3000/v1",\n api_key="sk-rt-YOUR_PROJECT_TOKEN",\n)\n\n# Non-streaming\nresponse = client.chat.completions.create(\n model="gpt-5-mini",\n messages=[{"role": "user", "content": "Hello!"}],\n)\nprint(response.choices[0].message.content)\n\n# Streaming\nstream = client.chat.completions.create(\n model="gpt-5-mini",\n messages=[{"role": "user", "content": "Tell me a story."}],\n stream=True,\n)\nfor chunk in stream:\n print(chunk.choices[0].delta.content or "", end="", flush=True)\n'})}),"\n",(0,a.jsx)(n.hr,{}),"\n",(0,a.jsx)(n.h2,{id:"anthropic-sdk",children:"Anthropic SDK"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-bash",children:"pip install anthropic\n"})}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-python",children:'import anthropic\n\nclient = anthropic.Anthropic(\n base_url="http://localhost:3000",\n api_key="sk-rt-YOUR_PROJECT_TOKEN",\n)\n\n# Non-streaming\nmessage = client.messages.create(\n model="claude-haiku-4-5",\n max_tokens=1024,\n messages=[{"role": "user", "content": "Hello, Claude!"}],\n)\nprint(message.content[0].text)\n\n# Streaming\nwith client.messages.stream(\n model="claude-haiku-4-5",\n max_tokens=1024,\n messages=[{"role": "user", "content": "Tell me a story."}],\n) as stream:\n for text in stream.text_stream:\n print(text, end="", flush=True)\n'})}),"\n",(0,a.jsx)(n.hr,{}),"\n",(0,a.jsx)(n.h2,{id:"raw-http-httpx",children:"Raw HTTP (httpx)"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-bash",children:"pip install httpx\n"})}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-python",children:'import httpx\nimport json\n\nHEADERS = {\n "Content-Type": "application/json",\n "Authorization": "Bearer sk-rt-YOUR_PROJECT_TOKEN",\n}\n\n# Non-streaming\nwith httpx.Client() as client:\n r = client.post(\n "http://localhost:3000/v1/chat/completions",\n headers=HEADERS,\n json={\n "model": "gpt-5-mini",\n "messages": [{"role": "user", "content": "Hello!"}],\n },\n )\n r.raise_for_status()\n print(r.json()["choices"][0]["message"]["content"])\n\n# Streaming\nwith httpx.Client() as client:\n with client.stream(\n "POST",\n "http://localhost:3000/v1/chat/completions",\n headers=HEADERS,\n json={\n "model": "gpt-5-mini",\n "messages": [{"role": "user", "content": "Tell me a story."}],\n "stream": True,\n },\n ) as r:\n for line in r.iter_lines():\n if not line.startswith("data: ") or line == "data: [DONE]":\n continue\n data = json.loads(line[6:])\n delta = data.get("choices", [{}])[0].get("delta", {}).get("content", "")\n print(delta, end="", flush=True)\n'})}),"\n",(0,a.jsxs)(n.admonition,{type:"tip",children:[(0,a.jsxs)(n.p,{children:["Use ",(0,a.jsx)(n.code,{children:"ROUTERLY_API_KEY"})," as an environment variable instead of hardcoding the token:"]}),(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-python",children:'import os\nclient = OpenAI(base_url="http://localhost:3000/v1", api_key=os.environ["ROUTERLY_API_KEY"])\n'})})]})]})}function h(e={}){const{wrapper:n}={...(0,o.R)(),...e.components};return n?(0,a.jsx)(n,{...e,children:(0,a.jsx)(p,{...e})}):p(e)}},8453(e,n,t){t.d(n,{R:()=>r,x:()=>i});var s=t(6540);const a={},o=s.createContext(a);function r(e){const n=s.useContext(o);return s.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function i(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(a):e.components||a:r(e.components),s.createElement(o.Provider,{value:n},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.