{"id":176294,"date":"2025-10-25T14:57:45","date_gmt":"2025-10-25T14:57:45","guid":{"rendered":"https:\/\/ktromedia.com\/?p=176294"},"modified":"2025-10-25T14:57:45","modified_gmt":"2025-10-25T14:57:45","slug":"10-python-one-liners-for-calling-llms-from-your-code","status":"publish","type":"post","link":"https:\/\/ktromedia.com\/?p=176294","title":{"rendered":"10 Python One-Liners for Calling LLMs from Your Code"},"content":{"rendered":"<div id=\"\">\n<div style=\"width: 810px\" class=\"wp-caption aligncenter\"><\/p>\n<p class=\"wp-caption-text\">Image by Author<\/p>\n<\/div>\n<h2>Introduction<\/h2>\n<p>You don\u2019t always need a heavy wrapper, a big client class, or dozens of lines of boilerplate to call a large language model. Sometimes one well-crafted line of Python does all the work: send a prompt, receive a response. That kind of simplicity can speed up prototyping or embedding LLM calls inside scripts or pipelines without architectural overhead.<\/p>\n<p>In this article, you\u2019ll see ten Python one-liners that call and interact with LLMs. We will cover:<\/p>\n<p>Each snippet comes with a brief explanation and a link to official documentation, so you can verify what\u2019s happening under the hood. By the end, you\u2019ll know not only how to drop in fast LLM calls but also understand when and why each pattern works.<\/p>\n<h2>Setting Up<\/h2>\n<p>Before dropping in the one-liners, there are a few things to prepare so they run smoothly:<\/p>\n<p>Install required packages (only once):<\/p>\n<div id=\"urvanov-syntax-highlighter-68f27bee1c035970621871\" class=\"urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover disable-anim\" style=\" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;\">\n<p><textarea wrap=\"soft\" class=\"urvanov-syntax-highlighter-plain print-no\" data-settings=\"dblclick\" readonly=\"readonly\" style=\"-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;\"><br \/>\n pip install openai anthropic google-generativeai requests httpx<\/textarea><\/p>\n<div class=\"urvanov-syntax-highlighter-main\" style=\"\">\n<table class=\"crayon-table\">\n<tr class=\"urvanov-syntax-highlighter-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<\/td>\n<td class=\"urvanov-syntax-highlighter-code\">\n<div class=\"crayon-pre\" style=\"font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;\">\n<p><span class=\"crayon-h\"> <\/span><span class=\"crayon-e\">pip <\/span><span class=\"crayon-e\">install <\/span><span class=\"crayon-e\">openai <\/span><span class=\"crayon-e\">anthropic <\/span><span class=\"crayon-v\">google<\/span><span class=\"crayon-o\">&#8211;<\/span><span class=\"crayon-e\">generativeai <\/span><span class=\"crayon-e\">requests <\/span><span class=\"crayon-v\">httpx<\/span><\/p>\n<\/div>\n<\/td>\n<\/tr>\n<\/table><\/div>\n<\/p><\/div>\n<p>Ensure your <strong>API keys<\/strong> are set in environment variables, never hard-coded in your scripts. For example:<\/p>\n<div id=\"urvanov-syntax-highlighter-68f27bee1c03e049164616\" class=\"urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover disable-anim\" style=\" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;\">\n<p><textarea wrap=\"soft\" class=\"urvanov-syntax-highlighter-plain print-no\" data-settings=\"dblclick\" readonly=\"readonly\" style=\"-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;\"><br \/>\nexport OPENAI_API_KEY=&#8221;sk-&#8230;&#8221;  &#13;<br \/>\nexport ANTHROPIC_API_KEY=&#8221;claude-yourkey&#8221;&#13;<br \/>\nexport GOOGLE_API_KEY=&#8221;your_google_key&#8221;<\/textarea><\/p>\n<div class=\"urvanov-syntax-highlighter-main\" style=\"\">\n<table class=\"crayon-table\">\n<tr class=\"urvanov-syntax-highlighter-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<\/td>\n<td class=\"urvanov-syntax-highlighter-code\">\n<div class=\"crayon-pre\" style=\"font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;\">\n<p><span class=\"crayon-e\">export <\/span><span class=\"crayon-v\">OPENAI_API_KEY<\/span><span class=\"crayon-o\">=<\/span><span class=\"crayon-s\">&#8220;sk-&#8230;&#8221;<\/span><span class=\"crayon-h\">\u00a0\u00a0<\/span><\/p>\n<p><span class=\"crayon-e\">export <\/span><span class=\"crayon-v\">ANTHROPIC_API_KEY<\/span><span class=\"crayon-o\">=<\/span><span class=\"crayon-s\">&#8220;claude-yourkey&#8221;<\/span><\/p>\n<p><span class=\"crayon-e\">export <\/span><span class=\"crayon-v\">GOOGLE_API_KEY<\/span><span class=\"crayon-o\">=<\/span><span class=\"crayon-s\">&#8220;your_google_key&#8221;<\/span><\/p>\n<\/div>\n<\/td>\n<\/tr>\n<\/table><\/div>\n<\/p><\/div>\n<p>For local setups (<strong><a href=\"https:\/\/ollama.com\/\" target=\"_blank\">Ollama<\/a><\/strong>, <strong><a href=\"https:\/\/lmstudio.ai\/\" target=\"_blank\">LM Studio<\/a><\/strong>, <strong><a href=\"https:\/\/docs.vllm.ai\/en\/latest\/\" target=\"_blank\">vLLM<\/a><\/strong>), you need the model server running locally and listening on the correct port (for instance, Ollama\u2019s default REST API runs at <strong>http:\/\/localhost:11434<\/strong>).<\/p>\n<p>All one-liners assume you use the right model name and that the model is either accessible via cloud or locally. With that in place, you can paste each one-liner directly into your <strong>Python REPL<\/strong> or script and get a response, subject to quota or local resource limits.<\/p>\n<h2>Hosted API One-Liners (Cloud Models)<\/h2>\n<p>Hosted APIs are the easiest way to start using large language models. You don\u2019t have to run a model locally or worry about GPU memory; just install the client library, set your API key, and send a prompt. These APIs are maintained by the model providers themselves, so they\u2019re reliable, secure, and frequently updated.<\/p>\n<p>The following one-liners show how to call some of the most popular hosted models directly from Python. Each example sends a simple message to the model and prints the generated response.<\/p>\n<h3>1. OpenAI GPT Chat Completion<\/h3>\n<p>OpenAI\u2019s API gives access to GPT models like GPT-4o and GPT-4o-mini. The SDK handles everything from authentication to response parsing.<\/p>\n<div id=\"urvanov-syntax-highlighter-68f27bee1c041644481449\" class=\"urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover disable-anim\" style=\" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;\">\n<p><textarea wrap=\"soft\" class=\"urvanov-syntax-highlighter-plain print-no\" data-settings=\"dblclick\" readonly=\"readonly\" style=\"-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;\"><br \/>\nfrom openai import OpenAI; print(OpenAI().chat.completions.create(model=&#8221;gpt-4o-mini&#8221;, messages=[{&#8220;role&#8221;:&#8221;user&#8221;,&#8221;content&#8221;:&#8221;Explain vector similarity&#8221;}]).choices[0].message.content)<\/textarea><\/p>\n<div class=\"urvanov-syntax-highlighter-main\" style=\"\">\n<table class=\"crayon-table\">\n<tr class=\"urvanov-syntax-highlighter-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<\/td>\n<td class=\"urvanov-syntax-highlighter-code\">\n<div class=\"crayon-pre\" style=\"font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;\">\n<p><span class=\"crayon-e\">from <\/span><span class=\"crayon-e\">openai <\/span><span class=\"crayon-e\">import <\/span><span class=\"crayon-v\">OpenAI<\/span><span class=\"crayon-sy\">;<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-e\">print<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-e\">OpenAI<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-v\">chat<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-v\">completions<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">create<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-v\">model<\/span><span class=\"crayon-o\">=<\/span><span class=\"crayon-s\">&#8220;gpt-4o-mini&#8221;<\/span><span class=\"crayon-sy\">,<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-v\">messages<\/span><span class=\"crayon-o\">=<\/span><span class=\"crayon-sy\">[<\/span><span class=\"crayon-sy\">{<\/span><span class=\"crayon-s\">&#8220;role&#8221;<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-s\">&#8220;user&#8221;<\/span><span class=\"crayon-sy\">,<\/span><span class=\"crayon-s\">&#8220;content&#8221;<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-s\">&#8220;Explain vector similarity&#8221;<\/span><span class=\"crayon-sy\">}<\/span><span class=\"crayon-sy\">]<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-v\">choices<\/span><span class=\"crayon-sy\">[<\/span><span class=\"crayon-cn\">0<\/span><span class=\"crayon-sy\">]<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-v\">message<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-v\">content<\/span><span class=\"crayon-sy\">)<\/span><\/p>\n<\/div>\n<\/td>\n<\/tr>\n<\/table><\/div>\n<\/p><\/div>\n<p><strong>What it does:<\/strong> It creates a client, sends a message to <strong>GPT-4o-mini<\/strong>, and prints the model\u2019s reply.<\/p>\n<p><strong>Why it works:<\/strong> The <code>openai<\/code> Python package wraps the REST API cleanly. You only need your <code>OPENAI_API_KEY<\/code> set as an environment variable.<\/p>\n<p><strong>Documentation<\/strong>: <a href=\"https:\/\/platform.openai.com\/docs\/api-reference\/chat\" target=\"_blank\">OpenAI Chat Completions API<\/a><\/p>\n<h3>2. Anthropic Claude<\/h3>\n<p>Anthropic\u2019s Claude models (Claude 3, Claude 3.5 Sonnet, etc.) are known for their long context windows and detailed reasoning. Their Python SDK follows a similar chat-message format to OpenAI\u2019s.<\/p>\n<div id=\"urvanov-syntax-highlighter-68f27bee1c044715059401\" class=\"urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover disable-anim\" style=\" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;\">\n<p><textarea wrap=\"soft\" class=\"urvanov-syntax-highlighter-plain print-no\" data-settings=\"dblclick\" readonly=\"readonly\" style=\"-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;\"><br \/>\nfrom anthropic import Anthropic; print(Anthropic().messages.create(model=&#8221;claude-3-5-sonnet&#8221;, messages=[{&#8220;role&#8221;:&#8221;user&#8221;,&#8221;content&#8221;:&#8221;How does chain of thought prompting work?&#8221;}]).content[0].text)<\/textarea><\/p>\n<div class=\"urvanov-syntax-highlighter-main\" style=\"\">\n<table class=\"crayon-table\">\n<tr class=\"urvanov-syntax-highlighter-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<\/td>\n<td class=\"urvanov-syntax-highlighter-code\">\n<div class=\"crayon-pre\" style=\"font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;\">\n<p><span class=\"crayon-e\">from <\/span><span class=\"crayon-e\">anthropic <\/span><span class=\"crayon-e\">import <\/span><span class=\"crayon-v\">Anthropic<\/span><span class=\"crayon-sy\">;<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-e\">print<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-e\">Anthropic<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-v\">messages<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">create<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-v\">model<\/span><span class=\"crayon-o\">=<\/span><span class=\"crayon-s\">&#8220;claude-3-5-sonnet&#8221;<\/span><span class=\"crayon-sy\">,<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-v\">messages<\/span><span class=\"crayon-o\">=<\/span><span class=\"crayon-sy\">[<\/span><span class=\"crayon-sy\">{<\/span><span class=\"crayon-s\">&#8220;role&#8221;<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-s\">&#8220;user&#8221;<\/span><span class=\"crayon-sy\">,<\/span><span class=\"crayon-s\">&#8220;content&#8221;<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-s\">&#8220;How does chain of thought prompting work?&#8221;<\/span><span class=\"crayon-sy\">}<\/span><span class=\"crayon-sy\">]<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-v\">content<\/span><span class=\"crayon-sy\">[<\/span><span class=\"crayon-cn\">0<\/span><span class=\"crayon-sy\">]<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-v\">text<\/span><span class=\"crayon-sy\">)<\/span><\/p>\n<\/div>\n<\/td>\n<\/tr>\n<\/table><\/div>\n<\/p><\/div>\n<p><strong>What it does:<\/strong> Initializes the Claude client, sends a message, and prints the text of the first response block.<\/p>\n<p><strong>Why it works:<\/strong> The <code>.messages.create()<\/code> method uses a standard message schema (<strong>role<\/strong> + <strong>content<\/strong>), returning structured output that\u2019s easy to extract.<\/p>\n<p><strong>Documentation:<\/strong> <a href=\"https:\/\/docs.anthropic.com\/claude\/reference\/messages_post\" target=\"_blank\">Anthropic Claude API Reference<\/a><\/p>\n<h3>3. Google Gemini<\/h3>\n<p>Google\u2019s Gemini API (via the <code>google-generativeai<\/code> library) makes it simple to call multimodal and text models with minimal setup. The key difference is that Gemini\u2019s API treats every prompt as \u201c<strong>content generation<\/strong>,\u201d whether it\u2019s text, code, or reasoning.<\/p>\n<div id=\"urvanov-syntax-highlighter-68f27bee1c047650142868\" class=\"urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover disable-anim\" style=\" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;\">\n<p><textarea wrap=\"soft\" class=\"urvanov-syntax-highlighter-plain print-no\" data-settings=\"dblclick\" readonly=\"readonly\" style=\"-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;\"><br \/>\nimport os, google.generativeai as genai; genai.configure(api_key=os.getenv(&#8220;GOOGLE_API_KEY&#8221;)); print(genai.GenerativeModel(&#8220;gemini-1.5-flash&#8221;).generate_content(&#8220;Describe retrieval-augmented generation&#8221;).text)<\/textarea><\/p>\n<div class=\"urvanov-syntax-highlighter-main\" style=\"\">\n<table class=\"crayon-table\">\n<tr class=\"urvanov-syntax-highlighter-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<\/td>\n<td class=\"urvanov-syntax-highlighter-code\">\n<div class=\"crayon-pre\" style=\"font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;\">\n<p><span class=\"crayon-e\">import <\/span><span class=\"crayon-v\">os<\/span><span class=\"crayon-sy\">,<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-v\">google<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">generativeai <\/span><span class=\"crayon-st\">as<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-v\">genai<\/span><span class=\"crayon-sy\">;<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-v\">genai<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">configure<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-v\">api_key<\/span><span class=\"crayon-o\">=<\/span><span class=\"crayon-v\">os<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">getenv<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-s\">&#8220;GOOGLE_API_KEY&#8221;<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">;<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-e\">print<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-v\">genai<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">GenerativeModel<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-s\">&#8220;gemini-1.5-flash&#8221;<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">generate_content<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-s\">&#8220;Describe retrieval-augmented generation&#8221;<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-v\">text<\/span><span class=\"crayon-sy\">)<\/span><\/p>\n<\/div>\n<\/td>\n<\/tr>\n<\/table><\/div>\n<\/p><\/div>\n<p><strong>What it does:<\/strong> Calls the <strong>Gemini 1.5 Flash model<\/strong> to describe retrieval-augmented generation (RAG) and prints the returned text.<\/p>\n<p><strong>Why it works:<\/strong> <code>GenerativeModel()<\/code> sets the model name, and <code>generate_content()<\/code> handles the prompt\/response flow. You just need your <code>GOOGLE_API_KEY<\/code> configured.<\/p>\n<p><strong>Documentation<\/strong>: <a href=\"https:\/\/ai.google.dev\/gemini-api\/docs\/quickstart\" target=\"_blank\">Google Gemini API Quickstart<\/a><\/p>\n<h3>4. Mistral AI (REST request)<\/h3>\n<p>Mistral provides a simple chat-completions REST API. You send a list of messages and receive a structured JSON response in return.<\/p>\n<div id=\"urvanov-syntax-highlighter-68f27bee1c049126396731\" class=\"urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover disable-anim\" style=\" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;\">\n<p><textarea wrap=\"soft\" class=\"urvanov-syntax-highlighter-plain print-no\" data-settings=\"dblclick\" readonly=\"readonly\" style=\"-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;\"><br \/>\nimport requests, json; print(requests.post(&#8220;https:\/\/api.mistral.ai\/v1\/chat\/completions&#8221;, headers={&#8220;Authorization&#8221;:&#8221;Bearer YOUR_MISTRAL_API_KEY&#8221;}, json={&#8220;model&#8221;:&#8221;mistral-tiny&#8221;,&#8221;messages&#8221;:[{&#8220;role&#8221;:&#8221;user&#8221;,&#8221;content&#8221;:&#8221;Define fine-tuning&#8221;}]}).json()[&#8220;choices&#8221;][0][&#8220;message&#8221;][&#8220;content&#8221;])<\/textarea><\/p>\n<div class=\"urvanov-syntax-highlighter-main\" style=\"\">\n<table class=\"crayon-table\">\n<tr class=\"urvanov-syntax-highlighter-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<\/td>\n<td class=\"urvanov-syntax-highlighter-code\">\n<div class=\"crayon-pre\" style=\"font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;\">\n<p><span class=\"crayon-e\">import <\/span><span class=\"crayon-v\">requests<\/span><span class=\"crayon-sy\">,<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-v\">json<\/span><span class=\"crayon-sy\">;<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-e\">print<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-v\">requests<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">post<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-s\">&#8220;https:\/\/api.mistral.ai\/v1\/chat\/completions&#8221;<\/span><span class=\"crayon-sy\">,<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-v\">headers<\/span><span class=\"crayon-o\">=<\/span><span class=\"crayon-sy\">{<\/span><span class=\"crayon-s\">&#8220;Authorization&#8221;<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-s\">&#8220;Bearer YOUR_MISTRAL_API_KEY&#8221;<\/span><span class=\"crayon-sy\">}<\/span><span class=\"crayon-sy\">,<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-v\">json<\/span><span class=\"crayon-o\">=<\/span><span class=\"crayon-sy\">{<\/span><span class=\"crayon-s\">&#8220;model&#8221;<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-s\">&#8220;mistral-tiny&#8221;<\/span><span class=\"crayon-sy\">,<\/span><span class=\"crayon-s\">&#8220;messages&#8221;<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-sy\">[<\/span><span class=\"crayon-sy\">{<\/span><span class=\"crayon-s\">&#8220;role&#8221;<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-s\">&#8220;user&#8221;<\/span><span class=\"crayon-sy\">,<\/span><span class=\"crayon-s\">&#8220;content&#8221;<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-s\">&#8220;Define fine-tuning&#8221;<\/span><span class=\"crayon-sy\">}<\/span><span class=\"crayon-sy\">]<\/span><span class=\"crayon-sy\">}<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">json<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">[<\/span><span class=\"crayon-s\">&#8220;choices&#8221;<\/span><span class=\"crayon-sy\">]<\/span><span class=\"crayon-sy\">[<\/span><span class=\"crayon-cn\">0<\/span><span class=\"crayon-sy\">]<\/span><span class=\"crayon-sy\">[<\/span><span class=\"crayon-s\">&#8220;message&#8221;<\/span><span class=\"crayon-sy\">]<\/span><span class=\"crayon-sy\">[<\/span><span class=\"crayon-s\">&#8220;content&#8221;<\/span><span class=\"crayon-sy\">]<\/span><span class=\"crayon-sy\">)<\/span><\/p>\n<\/div>\n<\/td>\n<\/tr>\n<\/table><\/div>\n<\/p><\/div>\n<p><strong>What it does:<\/strong> Posts a chat request to Mistral\u2019s API and prints the assistant message.<\/p>\n<p><strong>Why it works:<\/strong> The endpoint accepts an OpenAI-style messages array and returns <code>choices -&gt; message -&gt; content<\/code>.<br \/>Check out the Mistral API <a href=\"https:\/\/docs.mistral.ai\/api\/\" target=\"_blank\">reference and quickstart<\/a>.<\/p>\n<h3>5. Hugging Face Inference API<\/h3>\n<p>If you host a model or use a public one on Hugging Face, you can call it with a single POST. The <code>text-generation<\/code> task returns generated text in JSON.<\/p>\n<div id=\"urvanov-syntax-highlighter-68f27bee1c04c552479421\" class=\"urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover disable-anim\" style=\" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;\">\n<p><textarea wrap=\"soft\" class=\"urvanov-syntax-highlighter-plain print-no\" data-settings=\"dblclick\" readonly=\"readonly\" style=\"-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;\"><br \/>\nimport requests; print(requests.post(&#8220;https:\/\/api-inference.huggingface.co\/models\/mistralai\/Mistral-7B-Instruct-v0.2&#8221;, headers={&#8220;Authorization&#8221;:&#8221;Bearer YOUR_HF_TOKEN&#8221;}, json={&#8220;inputs&#8221;:&#8221;Write a haiku about data&#8221;}).json()[0][&#8220;generated_text&#8221;])<\/textarea><\/p>\n<div class=\"urvanov-syntax-highlighter-main\" style=\"\">\n<table class=\"crayon-table\">\n<tr class=\"urvanov-syntax-highlighter-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<\/td>\n<td class=\"urvanov-syntax-highlighter-code\">\n<div class=\"crayon-pre\" style=\"font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;\">\n<p><span class=\"crayon-e\">import <\/span><span class=\"crayon-v\">requests<\/span><span class=\"crayon-sy\">;<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-e\">print<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-v\">requests<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">post<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-s\">&#8220;https:\/\/api-inference.huggingface.co\/models\/mistralai\/Mistral-7B-Instruct-v0.2&#8221;<\/span><span class=\"crayon-sy\">,<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-v\">headers<\/span><span class=\"crayon-o\">=<\/span><span class=\"crayon-sy\">{<\/span><span class=\"crayon-s\">&#8220;Authorization&#8221;<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-s\">&#8220;Bearer YOUR_HF_TOKEN&#8221;<\/span><span class=\"crayon-sy\">}<\/span><span class=\"crayon-sy\">,<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-v\">json<\/span><span class=\"crayon-o\">=<\/span><span class=\"crayon-sy\">{<\/span><span class=\"crayon-s\">&#8220;inputs&#8221;<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-s\">&#8220;Write a haiku about data&#8221;<\/span><span class=\"crayon-sy\">}<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">json<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">[<\/span><span class=\"crayon-cn\">0<\/span><span class=\"crayon-sy\">]<\/span><span class=\"crayon-sy\">[<\/span><span class=\"crayon-s\">&#8220;generated_text&#8221;<\/span><span class=\"crayon-sy\">]<\/span><span class=\"crayon-sy\">)<\/span><\/p>\n<\/div>\n<\/td>\n<\/tr>\n<\/table><\/div>\n<\/p><\/div>\n<p><strong>What it does:<\/strong> Sends a prompt to a hosted model on Hugging Face and prints the generated text.<\/p>\n<p><strong>Why it works:<\/strong> The Inference API exposes task-specific endpoints; for text generation, it returns a list with <code>generated_text<\/code>.<br \/>Documentation: Inference API and Text Generation <a href=\"https:\/\/huggingface.co\/docs\/huggingface_hub\/en\/guides\/inference_endpoints\" target=\"_blank\">task pages<\/a>.<\/p>\n<h2>Local Model One-Liners<\/h2>\n<p>Running models on your machine gives you privacy and control. You avoid network latency and keep data local. The tradeoff is set up: you need the server running and a model pulled. The one-liners below assume you have already started the local service.<\/p>\n<h3>6. Ollama (Local Llama 3 or Mistral)<\/h3>\n<p>Ollama exposes a simple REST API on <strong>localhost:11434<\/strong>. Use <code>\/api\/generate<\/code> for prompt-style generation or <code>\/api\/chat<\/code> for chat turns.<\/p>\n<div id=\"urvanov-syntax-highlighter-68f27bee1c04e368317706\" class=\"urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover disable-anim\" style=\" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;\">\n<p><textarea wrap=\"soft\" class=\"urvanov-syntax-highlighter-plain print-no\" data-settings=\"dblclick\" readonly=\"readonly\" style=\"-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;\"><br \/>\nimport requests; print(requests.post(&#8220;http:\/\/localhost:11434\/api\/generate&#8221;, json={&#8220;model&#8221;:&#8221;llama3&#8243;,&#8221;prompt&#8221;:&#8221;What is vector search?&#8221;}).text)<\/textarea><\/p>\n<div class=\"urvanov-syntax-highlighter-main\" style=\"\">\n<table class=\"crayon-table\">\n<tr class=\"urvanov-syntax-highlighter-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<\/td>\n<td class=\"urvanov-syntax-highlighter-code\">\n<div class=\"crayon-pre\" style=\"font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;\">\n<p><span class=\"crayon-e\">import <\/span><span class=\"crayon-v\">requests<\/span><span class=\"crayon-sy\">;<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-e\">print<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-v\">requests<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">post<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-s\">&#8220;http:\/\/localhost:11434\/api\/generate&#8221;<\/span><span class=\"crayon-sy\">,<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-v\">json<\/span><span class=\"crayon-o\">=<\/span><span class=\"crayon-sy\">{<\/span><span class=\"crayon-s\">&#8220;model&#8221;<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-s\">&#8220;llama3&#8221;<\/span><span class=\"crayon-sy\">,<\/span><span class=\"crayon-s\">&#8220;prompt&#8221;<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-s\">&#8220;What is vector search?&#8221;<\/span><span class=\"crayon-sy\">}<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-v\">text<\/span><span class=\"crayon-sy\">)<\/span><\/p>\n<\/div>\n<\/td>\n<\/tr>\n<\/table><\/div>\n<\/p><\/div>\n<p><strong>What it does:<\/strong> Sends a generate request to your local Ollama server and prints the raw response text.<\/p>\n<p><strong>Why it works:<\/strong> Ollama runs a local HTTP server with endpoints like <code>\/api\/generate<\/code> and <code>\/api\/chat<\/code>. You must have the app running and the model pulled first. See official <strong><a href=\"https:\/\/docs.ollama.com\/api\" target=\"_blank\">API documentation<\/a><\/strong>. <\/p>\n<h3>7. LM Studio (OpenAI-Compatible Endpoint)<\/h3>\n<p>LM Studio can serve local models behind OpenAI-style endpoints such as <code>\/v1\/chat\/completions<\/code>. Start the server from the Developer tab, then call it like any OpenAI-compatible backend.<\/p>\n<div id=\"urvanov-syntax-highlighter-68f27bee1c050368186573\" class=\"urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover disable-anim\" style=\" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;\">\n<p><textarea wrap=\"soft\" class=\"urvanov-syntax-highlighter-plain print-no\" data-settings=\"dblclick\" readonly=\"readonly\" style=\"-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;\"><br \/>\nimport requests; print(requests.post(&#8220;http:\/\/localhost:1234\/v1\/chat\/completions&#8221;, json={&#8220;model&#8221;:&#8221;phi-3&#8243;,&#8221;messages&#8221;:[{&#8220;role&#8221;:&#8221;user&#8221;,&#8221;content&#8221;:&#8221;Explain embeddings&#8221;}]}).json()[&#8220;choices&#8221;][0][&#8220;message&#8221;][&#8220;content&#8221;])<\/textarea><\/p>\n<div class=\"urvanov-syntax-highlighter-main\" style=\"\">\n<table class=\"crayon-table\">\n<tr class=\"urvanov-syntax-highlighter-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<\/td>\n<td class=\"urvanov-syntax-highlighter-code\">\n<div class=\"crayon-pre\" style=\"font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;\">\n<p><span class=\"crayon-e\">import <\/span><span class=\"crayon-v\">requests<\/span><span class=\"crayon-sy\">;<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-e\">print<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-v\">requests<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">post<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-s\">&#8220;http:\/\/localhost:1234\/v1\/chat\/completions&#8221;<\/span><span class=\"crayon-sy\">,<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-v\">json<\/span><span class=\"crayon-o\">=<\/span><span class=\"crayon-sy\">{<\/span><span class=\"crayon-s\">&#8220;model&#8221;<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-s\">&#8220;phi-3&#8221;<\/span><span class=\"crayon-sy\">,<\/span><span class=\"crayon-s\">&#8220;messages&#8221;<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-sy\">[<\/span><span class=\"crayon-sy\">{<\/span><span class=\"crayon-s\">&#8220;role&#8221;<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-s\">&#8220;user&#8221;<\/span><span class=\"crayon-sy\">,<\/span><span class=\"crayon-s\">&#8220;content&#8221;<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-s\">&#8220;Explain embeddings&#8221;<\/span><span class=\"crayon-sy\">}<\/span><span class=\"crayon-sy\">]<\/span><span class=\"crayon-sy\">}<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">json<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">[<\/span><span class=\"crayon-s\">&#8220;choices&#8221;<\/span><span class=\"crayon-sy\">]<\/span><span class=\"crayon-sy\">[<\/span><span class=\"crayon-cn\">0<\/span><span class=\"crayon-sy\">]<\/span><span class=\"crayon-sy\">[<\/span><span class=\"crayon-s\">&#8220;message&#8221;<\/span><span class=\"crayon-sy\">]<\/span><span class=\"crayon-sy\">[<\/span><span class=\"crayon-s\">&#8220;content&#8221;<\/span><span class=\"crayon-sy\">]<\/span><span class=\"crayon-sy\">)<\/span><\/p>\n<\/div>\n<\/td>\n<\/tr>\n<\/table><\/div>\n<\/p><\/div>\n<p><strong>What it does:<\/strong> Calls a local chat completion and prints the message content.<\/p>\n<p><strong>Why it works:<\/strong> LM Studio exposes OpenAI-compatible routes and also supports an enhanced API. Recent releases also add <code>\/v1\/responses<\/code> support. Check the docs if your local build uses a different route. <\/p>\n<h3>8. vLLM (Self-Hosted LLM Server)<\/h3>\n<p>vLLM provides a high-performance server with OpenAI-compatible APIs. You can run it locally or on a GPU box, then call <code>\/v1\/chat\/completions<\/code>.<\/p>\n<div id=\"urvanov-syntax-highlighter-68f27bee1c053249377576\" class=\"urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover disable-anim\" style=\" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;\">\n<p><textarea wrap=\"soft\" class=\"urvanov-syntax-highlighter-plain print-no\" data-settings=\"dblclick\" readonly=\"readonly\" style=\"-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;\"><br \/>\nimport requests; print(requests.post(&#8220;http:\/\/localhost:8000\/v1\/chat\/completions&#8221;, json={&#8220;model&#8221;:&#8221;mistral&#8221;,&#8221;messages&#8221;:[{&#8220;role&#8221;:&#8221;user&#8221;,&#8221;content&#8221;:&#8221;Give me three LLM optimization tricks&#8221;}]}).json()[&#8220;choices&#8221;][0][&#8220;message&#8221;][&#8220;content&#8221;])<\/textarea><\/p>\n<div class=\"urvanov-syntax-highlighter-main\" style=\"\">\n<table class=\"crayon-table\">\n<tr class=\"urvanov-syntax-highlighter-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<\/td>\n<td class=\"urvanov-syntax-highlighter-code\">\n<div class=\"crayon-pre\" style=\"font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;\">\n<p><span class=\"crayon-e\">import <\/span><span class=\"crayon-v\">requests<\/span><span class=\"crayon-sy\">;<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-e\">print<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-v\">requests<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">post<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-s\">&#8220;http:\/\/localhost:8000\/v1\/chat\/completions&#8221;<\/span><span class=\"crayon-sy\">,<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-v\">json<\/span><span class=\"crayon-o\">=<\/span><span class=\"crayon-sy\">{<\/span><span class=\"crayon-s\">&#8220;model&#8221;<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-s\">&#8220;mistral&#8221;<\/span><span class=\"crayon-sy\">,<\/span><span class=\"crayon-s\">&#8220;messages&#8221;<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-sy\">[<\/span><span class=\"crayon-sy\">{<\/span><span class=\"crayon-s\">&#8220;role&#8221;<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-s\">&#8220;user&#8221;<\/span><span class=\"crayon-sy\">,<\/span><span class=\"crayon-s\">&#8220;content&#8221;<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-s\">&#8220;Give me three LLM optimization tricks&#8221;<\/span><span class=\"crayon-sy\">}<\/span><span class=\"crayon-sy\">]<\/span><span class=\"crayon-sy\">}<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">json<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">[<\/span><span class=\"crayon-s\">&#8220;choices&#8221;<\/span><span class=\"crayon-sy\">]<\/span><span class=\"crayon-sy\">[<\/span><span class=\"crayon-cn\">0<\/span><span class=\"crayon-sy\">]<\/span><span class=\"crayon-sy\">[<\/span><span class=\"crayon-s\">&#8220;message&#8221;<\/span><span class=\"crayon-sy\">]<\/span><span class=\"crayon-sy\">[<\/span><span class=\"crayon-s\">&#8220;content&#8221;<\/span><span class=\"crayon-sy\">]<\/span><span class=\"crayon-sy\">)<\/span><\/p>\n<\/div>\n<\/td>\n<\/tr>\n<\/table><\/div>\n<\/p><\/div>\n<p><strong>What it does:<\/strong> Sends a chat request to a vLLM server and prints the first response message.<\/p>\n<p><strong>Why it works:<\/strong> vLLM implements OpenAI-compatible Chat and Completions APIs, so any OpenAI-style client or plain requests call works once the server is running. Check the <strong><a href=\"https:\/\/docs.vllm.ai\/en\/latest\/serving\/openai_compatible_server.html\" target=\"_blank\">documentation<\/a><\/strong>.<\/p>\n<h2>Handy Tricks and Tips<\/h2>\n<p>Once you know the basics of sending requests to LLMs, a few neat tricks make your workflow faster and smoother. These final two examples demonstrate how to <strong>stream responses<\/strong> in real-time and how to <strong>execute asynchronous API calls<\/strong> without blocking your program.<\/p>\n<h3>9. Streaming Responses from OpenAI<\/h3>\n<p>Streaming allows you to print each token as it is generated by the model, rather than waiting for the full message. It\u2019s perfect for interactive apps or CLI tools where you want output to appear instantly.<\/p>\n<div id=\"urvanov-syntax-highlighter-68f27bee1c060042795786\" class=\"urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover disable-anim\" style=\" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;\">\n<p><textarea wrap=\"soft\" class=\"urvanov-syntax-highlighter-plain print-no\" data-settings=\"dblclick\" readonly=\"readonly\" style=\"-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;\"><br \/>\nfrom openai import OpenAI; [print(c.choices[0].delta.content or &#8220;&#8221;, end=&#8221;&#8221;) for c in OpenAI().chat.completions.create(model=&#8221;gpt-4o-mini&#8221;, messages=[{&#8220;role&#8221;:&#8221;user&#8221;,&#8221;content&#8221;:&#8221;Stream a poem&#8221;}], stream=True)]<\/textarea><\/p>\n<div class=\"urvanov-syntax-highlighter-main\" style=\"\">\n<table class=\"crayon-table\">\n<tr class=\"urvanov-syntax-highlighter-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<\/td>\n<td class=\"urvanov-syntax-highlighter-code\">\n<div class=\"crayon-pre\" style=\"font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;\">\n<p><span class=\"crayon-e\">from <\/span><span class=\"crayon-e\">openai <\/span><span class=\"crayon-e\">import <\/span><span class=\"crayon-v\">OpenAI<\/span><span class=\"crayon-sy\">;<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-sy\">[<\/span><span class=\"crayon-e\">print<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-v\">c<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-v\">choices<\/span><span class=\"crayon-sy\">[<\/span><span class=\"crayon-cn\">0<\/span><span class=\"crayon-sy\">]<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-v\">delta<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">content <\/span><span class=\"crayon-st\">or<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-s\">&#8220;&#8221;<\/span><span class=\"crayon-sy\">,<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-st\">end<\/span><span class=\"crayon-o\">=<\/span><span class=\"crayon-s\">&#8220;&#8221;<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-st\">for<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-i\">c<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-st\">in<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-e\">OpenAI<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-v\">chat<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-v\">completions<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">create<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-v\">model<\/span><span class=\"crayon-o\">=<\/span><span class=\"crayon-s\">&#8220;gpt-4o-mini&#8221;<\/span><span class=\"crayon-sy\">,<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-v\">messages<\/span><span class=\"crayon-o\">=<\/span><span class=\"crayon-sy\">[<\/span><span class=\"crayon-sy\">{<\/span><span class=\"crayon-s\">&#8220;role&#8221;<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-s\">&#8220;user&#8221;<\/span><span class=\"crayon-sy\">,<\/span><span class=\"crayon-s\">&#8220;content&#8221;<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-s\">&#8220;Stream a poem&#8221;<\/span><span class=\"crayon-sy\">}<\/span><span class=\"crayon-sy\">]<\/span><span class=\"crayon-sy\">,<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-v\">stream<\/span><span class=\"crayon-o\">=<\/span><span class=\"crayon-t\">True<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">]<\/span><\/p>\n<\/div>\n<\/td>\n<\/tr>\n<\/table><\/div>\n<\/p><\/div>\n<p><strong>What it does:<\/strong> Sends a prompt to <strong>GPT-4o-mini<\/strong> and prints tokens as they arrive, simulating a \u201c<strong>live typing<\/strong>\u201d effect.<\/p>\n<p><strong>Why it works:<\/strong> The <code>stream=True<\/code> flag in OpenAI\u2019s API returns partial events. Each <code>chunk<\/code> contains a <code>delta.content<\/code> field, which this one-liner prints as it streams in. <\/p>\n<p><strong>Documentation:<\/strong> <a href=\"https:\/\/platform.openai.com\/docs\/guides\/streaming\" target=\"_blank\">OpenAI Streaming Guide<\/a>.<\/p>\n<h3>10. Async Calls with httpx<\/h3>\n<p>Asynchronous calls enable you to query models without blocking your app, making them ideal for making multiple requests simultaneously or integrating LLMs into web servers.<\/p>\n<div id=\"urvanov-syntax-highlighter-68f27bee1c065855402560\" class=\"urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover disable-anim\" style=\" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;\">\n<p><textarea wrap=\"soft\" class=\"urvanov-syntax-highlighter-plain print-no\" data-settings=\"dblclick\" readonly=\"readonly\" style=\"-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;\"><br \/>\nimport asyncio, httpx; print(asyncio.run(httpx.AsyncClient().post(&#8220;https:\/\/api.mistral.ai\/v1\/chat\/completions&#8221;, headers={&#8220;Authorization&#8221;:&#8221;Bearer TOKEN&#8221;}, json={&#8220;model&#8221;:&#8221;mistral-tiny&#8221;,&#8221;messages&#8221;:[{&#8220;role&#8221;:&#8221;user&#8221;,&#8221;content&#8221;:&#8221;Hello&#8221;}]})).json()[&#8220;choices&#8221;][0][&#8220;message&#8221;][&#8220;content&#8221;])<\/textarea><\/p>\n<div class=\"urvanov-syntax-highlighter-main\" style=\"\">\n<table class=\"crayon-table\">\n<tr class=\"urvanov-syntax-highlighter-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<\/td>\n<td class=\"urvanov-syntax-highlighter-code\">\n<div class=\"crayon-pre\" style=\"font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;\">\n<p><span class=\"crayon-e\">import <\/span><span class=\"crayon-v\">asyncio<\/span><span class=\"crayon-sy\">,<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-v\">httpx<\/span><span class=\"crayon-sy\">;<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-e\">print<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-v\">asyncio<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">run<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-v\">httpx<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">AsyncClient<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">post<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-s\">&#8220;https:\/\/api.mistral.ai\/v1\/chat\/completions&#8221;<\/span><span class=\"crayon-sy\">,<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-v\">headers<\/span><span class=\"crayon-o\">=<\/span><span class=\"crayon-sy\">{<\/span><span class=\"crayon-s\">&#8220;Authorization&#8221;<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-s\">&#8220;Bearer TOKEN&#8221;<\/span><span class=\"crayon-sy\">}<\/span><span class=\"crayon-sy\">,<\/span><span class=\"crayon-h\"> <\/span><span class=\"crayon-v\">json<\/span><span class=\"crayon-o\">=<\/span><span class=\"crayon-sy\">{<\/span><span class=\"crayon-s\">&#8220;model&#8221;<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-s\">&#8220;mistral-tiny&#8221;<\/span><span class=\"crayon-sy\">,<\/span><span class=\"crayon-s\">&#8220;messages&#8221;<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-sy\">[<\/span><span class=\"crayon-sy\">{<\/span><span class=\"crayon-s\">&#8220;role&#8221;<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-s\">&#8220;user&#8221;<\/span><span class=\"crayon-sy\">,<\/span><span class=\"crayon-s\">&#8220;content&#8221;<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-s\">&#8220;Hello&#8221;<\/span><span class=\"crayon-sy\">}<\/span><span class=\"crayon-sy\">]<\/span><span class=\"crayon-sy\">}<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">json<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">[<\/span><span class=\"crayon-s\">&#8220;choices&#8221;<\/span><span class=\"crayon-sy\">]<\/span><span class=\"crayon-sy\">[<\/span><span class=\"crayon-cn\">0<\/span><span class=\"crayon-sy\">]<\/span><span class=\"crayon-sy\">[<\/span><span class=\"crayon-s\">&#8220;message&#8221;<\/span><span class=\"crayon-sy\">]<\/span><span class=\"crayon-sy\">[<\/span><span class=\"crayon-s\">&#8220;content&#8221;<\/span><span class=\"crayon-sy\">]<\/span><span class=\"crayon-sy\">)<\/span><\/p>\n<\/div>\n<\/td>\n<\/tr>\n<\/table><\/div>\n<\/p><\/div>\n<p><strong>What it does:<\/strong> Posts a chat request to Mistral\u2019s API asynchronously, then prints the model\u2019s reply once complete.<\/p>\n<p><strong>Why it works:<\/strong> The <code>httpx<\/code> library supports async <strong>I\/O<\/strong>, so network calls don\u2019t block the main thread. This pattern is handy for lightweight concurrency in scripts or apps.<\/p>\n<p><strong>Documentation:<\/strong> <a href=\"https:\/\/www.python-httpx.org\/async\/#making-asynchronous-requests\" target=\"_blank\">Async Support<\/a>.<\/p>\n<h2>Wrapping Up<\/h2>\n<p>Each of these one-liners is more than a quick demo; it\u2019s a building block. You can turn any of them into a function, wrap them inside a command-line tool, or build them into a backend service. The same code that fits on one line can easily expand into production workflows once you add error handling, caching, or logging.<\/p>\n<p>If you want to explore further, check the official documentation for detailed parameters like <strong>temperature<\/strong>, <strong>max tokens<\/strong>, and <strong>streaming options<\/strong>. Each provider maintains reliable references:<\/p>\n<p>The real takeaway is that <strong>Python makes working with LLMs both accessible and flexible<\/strong>. Whether you\u2019re running <strong>GPT-4o<\/strong> in the cloud or <strong>Llama 3<\/strong> locally, you can reach production-grade results with just a few lines of code.<\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Image by Author Introduction You don\u2019t always need a heavy wrapper, a big client class, or dozens of lines of boilerplate to call a large language model. Sometimes one well-crafted line of Python does all the work: send a prompt, receive a response. That kind of simplicity can speed up prototyping or embedding LLM calls<\/p>\n","protected":false},"author":1,"featured_media":176295,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[],"class_list":{"0":"post-176294","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-ai"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>10 Python One-Liners for Calling LLMs from Your Code - Ktromedia<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ktromedia.com\/?p=176294\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"10 Python One-Liners for Calling LLMs from Your Code - Ktromedia\" \/>\n<meta property=\"og:description\" content=\"Image by Author Introduction You don\u2019t always need a heavy wrapper, a big client class, or dozens of lines of boilerplate to call a large language model. Sometimes one well-crafted line of Python does all the work: send a prompt, receive a response. That kind of simplicity can speed up prototyping or embedding LLM calls\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ktromedia.com\/?p=176294\" \/>\n<meta property=\"og:site_name\" content=\"Ktromedia\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/KTROMedia\/\" \/>\n<meta property=\"article:published_time\" content=\"2025-10-25T14:57:45+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ktromedia.com\/wp-content\/uploads\/2025\/10\/1761404262_10-Python-One-Liners-for-Calling-LLMs-from-Your-Code.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"576\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"KTRO TEAM\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"KTRO TEAM\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/ktromedia.com\/?p=176294#article\",\"isPartOf\":{\"@id\":\"https:\/\/ktromedia.com\/?p=176294\"},\"author\":{\"name\":\"KTRO TEAM\",\"@id\":\"https:\/\/ktromedia.com\/#\/schema\/person\/612bf2fbac107722ea365932cdd35f5b\"},\"headline\":\"10 Python One-Liners for Calling LLMs from Your Code\",\"datePublished\":\"2025-10-25T14:57:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/ktromedia.com\/?p=176294\"},\"wordCount\":1883,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/ktromedia.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/ktromedia.com\/?p=176294#primaryimage\"},\"thumbnailUrl\":\"https:\/\/ktromedia.com\/wp-content\/uploads\/2025\/10\/1761404262_10-Python-One-Liners-for-Calling-LLMs-from-Your-Code.png\",\"articleSection\":[\"\u4eba\u5de5\u667a\u80fd\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/ktromedia.com\/?p=176294#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ktromedia.com\/?p=176294\",\"url\":\"https:\/\/ktromedia.com\/?p=176294\",\"name\":\"10 Python One-Liners for Calling LLMs from Your Code - Ktromedia\",\"isPartOf\":{\"@id\":\"https:\/\/ktromedia.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/ktromedia.com\/?p=176294#primaryimage\"},\"image\":{\"@id\":\"https:\/\/ktromedia.com\/?p=176294#primaryimage\"},\"thumbnailUrl\":\"https:\/\/ktromedia.com\/wp-content\/uploads\/2025\/10\/1761404262_10-Python-One-Liners-for-Calling-LLMs-from-Your-Code.png\",\"datePublished\":\"2025-10-25T14:57:45+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/ktromedia.com\/?p=176294#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/ktromedia.com\/?p=176294\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/ktromedia.com\/?p=176294#primaryimage\",\"url\":\"https:\/\/ktromedia.com\/wp-content\/uploads\/2025\/10\/1761404262_10-Python-One-Liners-for-Calling-LLMs-from-Your-Code.png\",\"contentUrl\":\"https:\/\/ktromedia.com\/wp-content\/uploads\/2025\/10\/1761404262_10-Python-One-Liners-for-Calling-LLMs-from-Your-Code.png\",\"width\":1024,\"height\":576},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/ktromedia.com\/?p=176294#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/ktromedia.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"10 Python One-Liners for Calling LLMs from Your Code\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/ktromedia.com\/#website\",\"url\":\"https:\/\/ktromedia.com\/\",\"name\":\"Ktromedia\",\"description\":\"KTRO MEDIA Crypto News\",\"publisher\":{\"@id\":\"https:\/\/ktromedia.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/ktromedia.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/ktromedia.com\/#organization\",\"name\":\"Ktromedia\",\"url\":\"https:\/\/ktromedia.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/ktromedia.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/ktromedia.com\/wp-content\/uploads\/2025\/11\/ktroicon.png\",\"contentUrl\":\"https:\/\/ktromedia.com\/wp-content\/uploads\/2025\/11\/ktroicon.png\",\"width\":250,\"height\":250,\"caption\":\"Ktromedia\"},\"image\":{\"@id\":\"https:\/\/ktromedia.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/KTROMedia\/\",\"https:\/\/www.linkedin.com\/company\/ktro-media\/\",\"https:\/\/t.me\/ktrogroup\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/ktromedia.com\/#\/schema\/person\/612bf2fbac107722ea365932cdd35f5b\",\"name\":\"KTRO TEAM\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/ktromedia.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/ktromedia.com\/wp-content\/uploads\/2025\/10\/cropped-Untitled-design-7-1-150x150.png\",\"contentUrl\":\"https:\/\/ktromedia.com\/wp-content\/uploads\/2025\/10\/cropped-Untitled-design-7-1-150x150.png\",\"caption\":\"KTRO TEAM\"},\"description\":\"KTRO MEDIA \u662f\u4e00\u5bb6\u5168\u7403\u6027\u7684\u534e\u6587WEB3\u5a92\u4f53\u516c\u53f8\u3002\u6211\u4eec\u81f4\u529b\u4e8e\u4e3a\u533a\u5757\u94fe\u548c\u91d1\u878d\u79d1\u6280\u9886\u57df\u63d0\u4f9b\u6700\u65b0\u7684\u65b0\u95fb\u3001\u89c1\u89e3\u548c\u8d8b\u52bf\u5206\u6790\u3002\u6211\u4eec\u7684\u5b97\u65e8\u662f\u4e3a\u5168\u7403\u7528\u6237\u63d0\u4f9b\u9ad8\u8d28\u91cf\u3001\u5168\u9762\u7684\u8d44\u8baf\u670d\u52a1\uff0c\u8ba9\u4ed6\u4eec\u66f4\u597d\u5730\u4e86\u89e3\u533a\u5757\u94fe\u548c\u91d1\u878d\u79d1\u6280\u884c\u4e1a\u7684\u6700\u65b0\u52a8\u6001\u3002\u6211\u4eec\u4e5f\u5e0c\u671b\u80fd\u5e2e\u5230\u66f4\u591a\u4f18\u79c0\u7684WEB3\u4ea7\u54c1\u627e\u5230\u66f4\u591a\u66f4\u597d\u7684\u8d44\u6e90\u597d\u8ba9\u8fd9\u9886\u57df\u53d8\u5f97\u66f4\u6210\u719f\u3002 \u6211\u4eec\u7684\u62a5\u9053\u8303\u56f4\u6db5\u76d6\u4e86\u533a\u5757\u94fe\u3001\u52a0\u5bc6\u8d27\u5e01\u3001\u667a\u80fd\u5408\u7ea6\u3001DeFi\u3001NFT \u548c Web3 \u751f\u6001\u7cfb\u7edf\u7b49\u9886\u57df\u3002\u6211\u4eec\u7684\u62a5\u9053\u4e0d\u4ec5\u6765\u81ea\u884c\u4e1a\u5185\u7684\u4e13\u5bb6\uff0c\u5148\u950b\u8005\u4e5f\u5305\u62ec\u4e86\u6211\u4eec\u81ea\u5df1\u7684\u5206\u6790\u548c\u89c2\u70b9\u3002\u6211\u4eec\u5728\u5404\u4e2a\u56fd\u5bb6\u548c\u5730\u533a\u90fd\u8bbe\u6709\u56e2\u961f\uff0c\u4e3a\u8bfb\u8005\u63d0\u4f9b\u672c\u5730\u5316\u7684\u62a5\u9053\u548c\u5206\u6790\u3002 \u9664\u4e86\u65b0\u95fb\u62a5\u9053\uff0c\u6211\u4eec\u8fd8\u63d0\u4f9b\u5e02\u573a\u7814\u7a76\u548c\u54a8\u8be2\u670d\u52a1\u3002\u6211\u4eec\u7684\u4e13\u4e1a\u56e2\u961f\u53ef\u4ee5\u4e3a\u60a8\u63d0\u4f9b\u6709\u5173\u533a\u5757\u94fe\u548c\u91d1\u878d\u79d1\u6280\u884c\u4e1a\u7684\u6df1\u5165\u5206\u6790\u548c\u5e02\u573a\u8d8b\u52bf\uff0c\u5e2e\u52a9\u60a8\u505a\u51fa\u66f4\u660e\u667a\u7684\u6295\u8d44\u51b3\u7b56\u3002 \u6211\u4eec\u7684\u4f7f\u547d\u662f\u6210\u4e3a\u5168\u7403\u534e\u6587\u533a\u5757\u94fe\u548c\u91d1\u878d\u79d1\u6280\u884c\u4e1a\u6700\u53d7\u4fe1\u8d56\u7684\u4fe1\u606f\u6765\u6e90\u4e4b\u4e00\u3002\u6211\u4eec\u5c06\u7ee7\u7eed\u4e0d\u65ad\u52aa\u529b\uff0c\u4e3a\u8bfb\u8005\u63d0\u4f9b\u6700\u65b0\u3001\u6700\u5168\u9762\u3001\u6700\u53ef\u9760\u7684\u4fe1\u606f\u670d\u52a1\u3002\",\"sameAs\":[\"https:\/\/ktromedia.com\"],\"url\":\"https:\/\/ktromedia.com\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"10 Python One-Liners for Calling LLMs from Your Code - Ktromedia","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/ktromedia.com\/?p=176294","og_locale":"en_US","og_type":"article","og_title":"10 Python One-Liners for Calling LLMs from Your Code - Ktromedia","og_description":"Image by Author Introduction You don\u2019t always need a heavy wrapper, a big client class, or dozens of lines of boilerplate to call a large language model. Sometimes one well-crafted line of Python does all the work: send a prompt, receive a response. That kind of simplicity can speed up prototyping or embedding LLM calls","og_url":"https:\/\/ktromedia.com\/?p=176294","og_site_name":"Ktromedia","article_publisher":"https:\/\/www.facebook.com\/KTROMedia\/","article_published_time":"2025-10-25T14:57:45+00:00","og_image":[{"width":1024,"height":576,"url":"https:\/\/ktromedia.com\/wp-content\/uploads\/2025\/10\/1761404262_10-Python-One-Liners-for-Calling-LLMs-from-Your-Code.png","type":"image\/png"}],"author":"KTRO TEAM","twitter_card":"summary_large_image","twitter_misc":{"Written by":"KTRO TEAM","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ktromedia.com\/?p=176294#article","isPartOf":{"@id":"https:\/\/ktromedia.com\/?p=176294"},"author":{"name":"KTRO TEAM","@id":"https:\/\/ktromedia.com\/#\/schema\/person\/612bf2fbac107722ea365932cdd35f5b"},"headline":"10 Python One-Liners for Calling LLMs from Your Code","datePublished":"2025-10-25T14:57:45+00:00","mainEntityOfPage":{"@id":"https:\/\/ktromedia.com\/?p=176294"},"wordCount":1883,"commentCount":0,"publisher":{"@id":"https:\/\/ktromedia.com\/#organization"},"image":{"@id":"https:\/\/ktromedia.com\/?p=176294#primaryimage"},"thumbnailUrl":"https:\/\/ktromedia.com\/wp-content\/uploads\/2025\/10\/1761404262_10-Python-One-Liners-for-Calling-LLMs-from-Your-Code.png","articleSection":["\u4eba\u5de5\u667a\u80fd"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ktromedia.com\/?p=176294#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ktromedia.com\/?p=176294","url":"https:\/\/ktromedia.com\/?p=176294","name":"10 Python One-Liners for Calling LLMs from Your Code - Ktromedia","isPartOf":{"@id":"https:\/\/ktromedia.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ktromedia.com\/?p=176294#primaryimage"},"image":{"@id":"https:\/\/ktromedia.com\/?p=176294#primaryimage"},"thumbnailUrl":"https:\/\/ktromedia.com\/wp-content\/uploads\/2025\/10\/1761404262_10-Python-One-Liners-for-Calling-LLMs-from-Your-Code.png","datePublished":"2025-10-25T14:57:45+00:00","breadcrumb":{"@id":"https:\/\/ktromedia.com\/?p=176294#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ktromedia.com\/?p=176294"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ktromedia.com\/?p=176294#primaryimage","url":"https:\/\/ktromedia.com\/wp-content\/uploads\/2025\/10\/1761404262_10-Python-One-Liners-for-Calling-LLMs-from-Your-Code.png","contentUrl":"https:\/\/ktromedia.com\/wp-content\/uploads\/2025\/10\/1761404262_10-Python-One-Liners-for-Calling-LLMs-from-Your-Code.png","width":1024,"height":576},{"@type":"BreadcrumbList","@id":"https:\/\/ktromedia.com\/?p=176294#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ktromedia.com\/"},{"@type":"ListItem","position":2,"name":"10 Python One-Liners for Calling LLMs from Your Code"}]},{"@type":"WebSite","@id":"https:\/\/ktromedia.com\/#website","url":"https:\/\/ktromedia.com\/","name":"Ktromedia","description":"KTRO MEDIA Crypto News","publisher":{"@id":"https:\/\/ktromedia.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ktromedia.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/ktromedia.com\/#organization","name":"Ktromedia","url":"https:\/\/ktromedia.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ktromedia.com\/#\/schema\/logo\/image\/","url":"https:\/\/ktromedia.com\/wp-content\/uploads\/2025\/11\/ktroicon.png","contentUrl":"https:\/\/ktromedia.com\/wp-content\/uploads\/2025\/11\/ktroicon.png","width":250,"height":250,"caption":"Ktromedia"},"image":{"@id":"https:\/\/ktromedia.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/KTROMedia\/","https:\/\/www.linkedin.com\/company\/ktro-media\/","https:\/\/t.me\/ktrogroup"]},{"@type":"Person","@id":"https:\/\/ktromedia.com\/#\/schema\/person\/612bf2fbac107722ea365932cdd35f5b","name":"KTRO TEAM","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ktromedia.com\/#\/schema\/person\/image\/","url":"https:\/\/ktromedia.com\/wp-content\/uploads\/2025\/10\/cropped-Untitled-design-7-1-150x150.png","contentUrl":"https:\/\/ktromedia.com\/wp-content\/uploads\/2025\/10\/cropped-Untitled-design-7-1-150x150.png","caption":"KTRO TEAM"},"description":"KTRO MEDIA \u662f\u4e00\u5bb6\u5168\u7403\u6027\u7684\u534e\u6587WEB3\u5a92\u4f53\u516c\u53f8\u3002\u6211\u4eec\u81f4\u529b\u4e8e\u4e3a\u533a\u5757\u94fe\u548c\u91d1\u878d\u79d1\u6280\u9886\u57df\u63d0\u4f9b\u6700\u65b0\u7684\u65b0\u95fb\u3001\u89c1\u89e3\u548c\u8d8b\u52bf\u5206\u6790\u3002\u6211\u4eec\u7684\u5b97\u65e8\u662f\u4e3a\u5168\u7403\u7528\u6237\u63d0\u4f9b\u9ad8\u8d28\u91cf\u3001\u5168\u9762\u7684\u8d44\u8baf\u670d\u52a1\uff0c\u8ba9\u4ed6\u4eec\u66f4\u597d\u5730\u4e86\u89e3\u533a\u5757\u94fe\u548c\u91d1\u878d\u79d1\u6280\u884c\u4e1a\u7684\u6700\u65b0\u52a8\u6001\u3002\u6211\u4eec\u4e5f\u5e0c\u671b\u80fd\u5e2e\u5230\u66f4\u591a\u4f18\u79c0\u7684WEB3\u4ea7\u54c1\u627e\u5230\u66f4\u591a\u66f4\u597d\u7684\u8d44\u6e90\u597d\u8ba9\u8fd9\u9886\u57df\u53d8\u5f97\u66f4\u6210\u719f\u3002 \u6211\u4eec\u7684\u62a5\u9053\u8303\u56f4\u6db5\u76d6\u4e86\u533a\u5757\u94fe\u3001\u52a0\u5bc6\u8d27\u5e01\u3001\u667a\u80fd\u5408\u7ea6\u3001DeFi\u3001NFT \u548c Web3 \u751f\u6001\u7cfb\u7edf\u7b49\u9886\u57df\u3002\u6211\u4eec\u7684\u62a5\u9053\u4e0d\u4ec5\u6765\u81ea\u884c\u4e1a\u5185\u7684\u4e13\u5bb6\uff0c\u5148\u950b\u8005\u4e5f\u5305\u62ec\u4e86\u6211\u4eec\u81ea\u5df1\u7684\u5206\u6790\u548c\u89c2\u70b9\u3002\u6211\u4eec\u5728\u5404\u4e2a\u56fd\u5bb6\u548c\u5730\u533a\u90fd\u8bbe\u6709\u56e2\u961f\uff0c\u4e3a\u8bfb\u8005\u63d0\u4f9b\u672c\u5730\u5316\u7684\u62a5\u9053\u548c\u5206\u6790\u3002 \u9664\u4e86\u65b0\u95fb\u62a5\u9053\uff0c\u6211\u4eec\u8fd8\u63d0\u4f9b\u5e02\u573a\u7814\u7a76\u548c\u54a8\u8be2\u670d\u52a1\u3002\u6211\u4eec\u7684\u4e13\u4e1a\u56e2\u961f\u53ef\u4ee5\u4e3a\u60a8\u63d0\u4f9b\u6709\u5173\u533a\u5757\u94fe\u548c\u91d1\u878d\u79d1\u6280\u884c\u4e1a\u7684\u6df1\u5165\u5206\u6790\u548c\u5e02\u573a\u8d8b\u52bf\uff0c\u5e2e\u52a9\u60a8\u505a\u51fa\u66f4\u660e\u667a\u7684\u6295\u8d44\u51b3\u7b56\u3002 \u6211\u4eec\u7684\u4f7f\u547d\u662f\u6210\u4e3a\u5168\u7403\u534e\u6587\u533a\u5757\u94fe\u548c\u91d1\u878d\u79d1\u6280\u884c\u4e1a\u6700\u53d7\u4fe1\u8d56\u7684\u4fe1\u606f\u6765\u6e90\u4e4b\u4e00\u3002\u6211\u4eec\u5c06\u7ee7\u7eed\u4e0d\u65ad\u52aa\u529b\uff0c\u4e3a\u8bfb\u8005\u63d0\u4f9b\u6700\u65b0\u3001\u6700\u5168\u9762\u3001\u6700\u53ef\u9760\u7684\u4fe1\u606f\u670d\u52a1\u3002","sameAs":["https:\/\/ktromedia.com"],"url":"https:\/\/ktromedia.com\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/ktromedia.com\/index.php?rest_route=\/wp\/v2\/posts\/176294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ktromedia.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ktromedia.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ktromedia.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ktromedia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=176294"}],"version-history":[{"count":1,"href":"https:\/\/ktromedia.com\/index.php?rest_route=\/wp\/v2\/posts\/176294\/revisions"}],"predecessor-version":[{"id":176296,"href":"https:\/\/ktromedia.com\/index.php?rest_route=\/wp\/v2\/posts\/176294\/revisions\/176296"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ktromedia.com\/index.php?rest_route=\/wp\/v2\/media\/176295"}],"wp:attachment":[{"href":"https:\/\/ktromedia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=176294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ktromedia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=176294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ktromedia.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=176294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}