Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Neuml Txtai OpenCode LLM

From Leeroopedia


Knowledge Sources
Domains Machine Learning, NLP, LLM, Code Generation
Last Updated 2026-02-10 01:00 GMT

Overview

Concrete tool for accessing OpenCode-compatible LLM servers via HTTP for generative text and code tasks provided by txtai.

Description

OpenCode is a generative model backend that extends the Generation base class and communicates with an OpenCode server over HTTP using the httpx library. It establishes a session with the server on initialization and sends message requests containing text prompts. The class supports specifying a provider and model extracted from the path string (e.g. "opencode/provider/model-id"). It defaults to connecting to a local opencode serve instance at http://localhost:4096. The static ismodel method detects paths starting with "opencode" (case-insensitive) while filtering out Hugging Face Hub models.

Usage

Use OpenCode when you want to connect txtai to an OpenCode-compatible server for code generation or general LLM tasks. It is automatically selected when the model path starts with "opencode".

Code Reference

Source Location

  • Repository: Neuml_Txtai
  • File: src/python/txtai/pipeline/llm/opencode.py

Signature

class OpenCode(Generation):
    @staticmethod
    def ismodel(path)
    @staticmethod
    def ishub(path)
    def __init__(self, path, template=None, **kwargs)
    def stream(self, texts, maxlength, stream, stop, **kwargs)

Import

from txtai.pipeline.llm.opencode import OpenCode

I/O Contract

Inputs

Name Type Required Description
path str Yes Model path starting with "opencode". Optionally includes provider/model (e.g. "opencode/provider/model-id").
template str No Prompt template string applied to text inputs.
url str No Base URL for the OpenCode server. Defaults to "http://localhost:4096".
kwargs dict No Additional keyword arguments.
texts list Yes (stream) List of prompts; each can be a string or a list of chat message dicts (content values are joined with newlines).
maxlength int Yes (stream) Maximum sequence length (passed to execute but not directly used by the HTTP API call).
stream bool Yes (stream) Stream flag (passed to execute).
stop list Yes (stream) List of stop strings (passed to execute).

Outputs

Name Type Description
result generator Yields generated text strings from the OpenCode server response. Each response contains text parts that are joined with newlines.

Usage Examples

from txtai.pipeline import LLM

# Connect to a local OpenCode server (default port 4096)
llm = LLM("opencode")
result = llm("Write a Python function to sort a list")
print(result)

# Specify a provider and model
llm = LLM("opencode/anthropic/claude-3-sonnet", url="http://localhost:4096")
result = llm("Explain the difference between a list and a tuple in Python")

# Chat-style input
result = llm([
    {"role": "user", "content": "Refactor this function to use list comprehension"},
    {"role": "assistant", "content": "Sure, here is the refactored version:"}
])

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment