""" LLM API Tutorial: Beyond CLI Tools with NexaAPI ================================================ This Gradio app demonstrates NexaAPI as a programmatic alternative to CLI LLM tools like kpihx-ai. Links: - NexaAPI: https://nexa-api.com - RapidAPI: https://rapidapi.com/user/nexaquency - PyPI: pip install nexaapi | https://pypi.org/project/nexaapi/ - npm: npm install nexaapi | https://www.npmjs.com/package/nexaapi - kpihx-ai: https://pypi.org/project/kpihx-ai/ """ import gradio as gr from nexaapi import NexaAPI DESCRIPTION = """ # LLM API Tutorial: Beyond CLI Tools with NexaAPI **kpihx-ai** is a great terminal LLM chat tool. But for programmatic access, use **NexaAPI**. - 🌐 [nexa-api.com](https://nexa-api.com) - 🔌 [rapidapi.com/user/nexaquency](https://rapidapi.com/user/nexaquency) - 🐍 `pip install nexaapi` | [PyPI](https://pypi.org/project/nexaapi/) - 📦 `npm install nexaapi` | [npm](https://www.npmjs.com/package/nexaapi) """ MODELS = [ "gpt-4o", "gpt-4o-mini", "claude-3-haiku", "claude-3-sonnet", "gemini-pro", "mistral-7b", "llama-3-70b", ] def chat_with_llm(api_key: str, model: str, system_prompt: str, user_message: str) -> str: """ Chat with any LLM model via NexaAPI. This is the programmatic equivalent of: k-ai chat --model """ if not api_key or api_key == "YOUR_API_KEY": return "⚠️ Please enter your NexaAPI key. Get one free at https://nexa-api.com" if not user_message.strip(): return "⚠️ Please enter a message." try: client = NexaAPI(api_key=api_key) messages = [] if system_prompt.strip(): messages.append({"role": "system", "content": system_prompt}) messages.append({"role": "user", "content": user_message}) response = client.chat.completions.create( model=model, messages=messages, ) return response.choices[0].message.content except Exception as e: return f"❌ Error: {str(e)}\n\nGet your API key at: https://nexa-api.com" def batch_process(api_key: str, model: str, prompts_text: str) -> str: """ Batch process multiple prompts — impossible with CLI tools like kpihx-ai. """ if not api_key or api_key == "YOUR_API_KEY": return "⚠️ Please enter your NexaAPI key. Get one free at https://nexa-api.com" prompts = [p.strip() for p in prompts_text.strip().split("\n") if p.strip()] if not prompts: return "⚠️ Please enter at least one prompt (one per line)." try: client = NexaAPI(api_key=api_key) results = [] for i, prompt in enumerate(prompts, 1): response = client.chat.completions.create( model=model, messages=[{"role": "user", "content": prompt}], ) result = response.choices[0].message.content results.append(f"**[{i}] Prompt:** {prompt}\n**Response:** {result}\n") return "\n---\n".join(results) except Exception as e: return f"❌ Error: {str(e)}" # Build Gradio interface with gr.Blocks(title="LLM API Tutorial with NexaAPI") as demo: gr.Markdown(DESCRIPTION) with gr.Tab("💬 Chat"): gr.Markdown("### Chat with any LLM model via NexaAPI") with gr.Row(): api_key_input = gr.Textbox( label="NexaAPI Key", placeholder="Get free key at nexa-api.com", type="password" ) model_dropdown = gr.Dropdown( choices=MODELS, value="gpt-4o-mini", label="Model (56+ available)" ) system_prompt = gr.Textbox( label="System Prompt (optional)", placeholder="You are a helpful assistant.", lines=2 ) user_message = gr.Textbox( label="Your Message", placeholder="Ask anything...", lines=3 ) chat_btn = gr.Button("Send", variant="primary") chat_output = gr.Textbox(label="Response", lines=8) chat_btn.click( fn=chat_with_llm, inputs=[api_key_input, model_dropdown, system_prompt, user_message], outputs=chat_output ) with gr.Tab("📦 Batch Processing"): gr.Markdown("### Batch process multiple prompts — impossible with CLI tools!") with gr.Row(): batch_api_key = gr.Textbox( label="NexaAPI Key", placeholder="Get free key at nexa-api.com", type="password" ) batch_model = gr.Dropdown( choices=MODELS, value="gpt-4o-mini", label="Model" ) batch_prompts = gr.Textbox( label="Prompts (one per line)", placeholder="Summarize: The quick brown fox...\nTranslate to Spanish: Hello\nWrite a haiku about coding", lines=6 ) batch_btn = gr.Button("Process Batch", variant="primary") batch_output = gr.Markdown(label="Results") batch_btn.click( fn=batch_process, inputs=[batch_api_key, batch_model, batch_prompts], outputs=batch_output ) with gr.Tab("📖 Code Examples"): gr.Markdown(""" ### Python SDK ```bash pip install nexaapi ``` ```python from nexaapi import NexaAPI client = NexaAPI(api_key='YOUR_API_KEY') response = client.chat.completions.create( model='gpt-4o', messages=[ {'role': 'system', 'content': 'You are a helpful assistant.'}, {'role': 'user', 'content': 'Explain quantum computing in simple terms.'} ] ) print(response.choices[0].message.content) ``` ### JavaScript SDK ```bash npm install nexaapi ``` ```javascript import NexaAPI from 'nexaapi'; const client = new NexaAPI({ apiKey: 'YOUR_API_KEY' }); const response = await client.chat.completions.create({ model: 'gpt-4o', messages: [ { role: 'user', content: 'What are the top AI trends in 2026?' } ] }); console.log(response.choices[0].message.content); ``` ### Links - 🌐 **NexaAPI**: [nexa-api.com](https://nexa-api.com) - 🔌 **RapidAPI**: [rapidapi.com/user/nexaquency](https://rapidapi.com/user/nexaquency) - 🐍 **PyPI**: [pypi.org/project/nexaapi/](https://pypi.org/project/nexaapi/) - 📦 **npm**: [npmjs.com/package/nexaapi](https://www.npmjs.com/package/nexaapi) - 🔧 **kpihx-ai**: [pypi.org/project/kpihx-ai/](https://pypi.org/project/kpihx-ai/) """) if __name__ == "__main__": demo.launch()