Skip to content

Documentation

Samuel Ikenna Great edited this page Dec 2, 2025 · 2 revisions

API Integration Examples

This document provides multiple ways to connect to Nectar-X-Studio, including Local Engine (BCI), HTTP GET/POST, WebSocket communication, and integration inside a Python application.


πŸ“‘ Local Engine (BCI) Example

def send_to_BCI(question):
    # BCI STANDS FOR BROAD CAST INTERFACE.
    import socket
    try:
        client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        client.connect(("127.0.0.1", 8012))
        client.send(question.encode("utf-8"))
        response = client.recv(4096)
        client.close()
        return response.decode("utf-8")
    except (socket.error, socket.timeout) as e:
        return f"[Error] Could not connect to AlphaLLM: {e}"

🌐 GET Request Example

import requests

url = "http://127.0.0.1:8000/predict"
params = {"data": "Hello AlphaLLM"}

response = requests.get(url, params=params)
print(response.json())

🌐 POST Request Example

import requests

url = "http://127.0.0.1:8000/predict"
payload = {"data": "Hello AlphaLLM"}

response = requests.post(url, json=payload)
print(response.json())

πŸ”Œ WebSocket Example

import asyncio
import websockets

async def send_data():
    url = "ws://127.0.0.1:8000/ws_predict"
    async with websockets.connect(url) as ws:
        await ws.send("Hello AlphaLLM")
        response = await ws.recv()
        print(response)

asyncio.run(send_data())

🧩 Integration in Your Application

import requests

# Replace with your actual token
ACCESS_TOKEN = "AccessToken"

def query_alpha_llm(data: str):
    url = "http://127.0.0.1:8000/predict"
    headers = {"Authorization": f"Bearer {ACCESS_TOKEN}"}
    try:
        # Send data as query parameter (server uses GET)
        response = requests.get(url, params={"data": data}, headers=headers)
        return response.json().get("response", "No response received")
    except requests.exceptions.RequestException as e:
        return f"Error: {e}"

def chat_terminal():
    print("Alpha LLM Terminal Chat (type 'exit' to quit)")
    while True:
        user_input = input("You: ").strip()
        if user_input.lower() == "exit":
            print("Exiting chat...")
            break
        response = query_alpha_llm(user_input)
        print(f"Alpha: {response}")

if __name__ == "__main__":
    chat_terminal()