Whitepaper
Docs
Sign In
Function
Function
pipe
v1.8.0
xAI
Function ID
xai
Creator
@olof
Downloads
73+
https://console.x.ai
Get
README
No README available
Function Code
Show
""" title: xAI description: https://console.x.ai author: Olof Larsson author_url: https://olof.tech version: 1.8.0 license: MIT """ import os import requests from typing import List, Union, Generator, Iterator, Optional from pydantic import BaseModel, Field class Pipe: class Valves(BaseModel): API_KEY: str = Field( default=os.getenv("XAI_API_KEY", ""), description="Get your key here: https://console.x.ai", ) def __init__(self): self.type = "manifold" self.id = "xai" self.name = "xai/" self.valves = self.Valves() self.base_url = "https://api.x.ai/v1" # Q: Why hard-code the model ids? # A: Because it yields the best performance. OpenWebUI is very sensitive to slowness here. It impacts page loads. def get_model_ids(self): # How to update: # curl "https://api.x.ai/v1/language-models" -H "Authorization: Bearer ${XAI_API_KEY}" | jq '.models | sort_by(.id) | map(.id)' return [ "grok-2-1212", "grok-2-vision-1212", "grok-3-beta", "grok-3-fast-beta", "grok-3-mini-beta", "grok-3-mini-fast-beta", "grok-beta", "grok-vision-beta", ] def pipes(self) -> List[dict]: model_ids = self.get_model_ids() return [{"id": model_id, "name": model_id} for model_id in model_ids] def pipe( self, body: dict, ) -> Union[str, Generator, Iterator]: body["model"] = body["model"].removeprefix(self.id + ".") try: r = requests.post( url=f"{self.base_url}/chat/completions", json=body, headers={ "Content-Type": "application/json", "Authorization": f"Bearer {self.valves.API_KEY}", }, stream=True, ) r.raise_for_status() if body["stream"]: return r.iter_lines() else: return r.json() except Exception as e: return f"Error: {e}"