LangChain with SovereignEG
Goal: call a SovereignEG model from LangChain, then build a small prompt chain.
Time: 5 minutes.
LangChain's ChatOpenAI class works with any OpenAI-compatible server. You only give it a base_url (https://backend.sovereigneg.com/v1), an api_key, and the model name.
Step 1 — Install
pip install langchain-openaiStep 2 — One chat call
Save as langchain_hello.py:
import os
import time
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="SovereignEG/Qwen3.8-27B-FP8",
base_url="https://backend.sovereigneg.com/v1",
api_key=os.environ["SOVEREIGNEG_API_KEY"],
)
start = time.time()
response = llm.invoke("Hello, who are you?")
elapsed = time.time() - start
print(response.content)
print(f"\nTime: {elapsed:.2f}s")
print(f"Tokens: {response.usage_metadata}")Run it:
python langchain_hello.pyYou get the answer, the time it took, and the token count (input_tokens, output_tokens, total_tokens) — this is what you are billed for.
Step 3 — Streaming
Print tokens as they arrive instead of waiting for the full answer:
for chunk in llm.stream("Write a short poem about the Nile."):
print(chunk.content, end="", flush=True)
print()Step 4 — A prompt chain
Chain a prompt template → model → string output with LCEL (the | operator):
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful assistant. Answer in {language}."),
("user", "{question}"),
])
chain = prompt | llm | StrOutputParser()
print(chain.invoke({"language": "Arabic", "question": "What is the capital of Egypt?"}))Step 5 — Useful options
llm = ChatOpenAI(
model="SovereignEG/Qwen3.8-27B-FP8",
base_url="https://backend.sovereigneg.com/v1",
api_key=os.environ["SOVEREIGNEG_API_KEY"],
temperature=0.2, # lower = more predictable
max_tokens=512, # cap the answer length (and cost)
timeout=60, # seconds
max_retries=2,
)JSON output (the model supports response_format):
json_llm = llm.bind(response_format={"type": "json_object"})
print(json_llm.invoke("Return a JSON object with keys city and country for Cairo.").content)Troubleshooting
| Problem | Fix |
|---|---|
AuthenticationError | SOVEREIGNEG_API_KEY is not set in this terminal |
BadRequestError … model_not_found | Use the exact id from /v1/models |
| Very slow first call | Normal: the first request warms up the connection; later calls are faster |
Next: LangGraph