API Documentation
/v1/completion/regex

/v1/completion/regex

This endpoint provides LLM completions that match specified regex patterns. It is an enhanced way of generating completions based on regular expression constraints.

Request: POST /v1/completion/regex

Headers

  • Content-Type: application/json
  • Authorization: Bearer <your_api_key>

Body

The request accepts a JSON body with the following properties:

FieldTypeDescriptionRequired
promptstringThe prompt to generate from.Yes
patternstring or string[]A single regex pattern to match.Yes
max_new_tokensintegerThe maximum number of tokens to generate. Defaults to 5.No
stop_after_matchbooleanWhether to stop generating after a match is found. Defaults to True.No
top_kintegerThe number of highest probability vocabulary tokens to keep for top-k-filtering. Defaults to 50.No
top_pfloatIf set to float < 1, only the most probable tokens with probabilities that add up to top_p or higher are kept for generation. Defaults to 1.0.No
temperaturefloatThe value used to modulate the next token probabilities. Defaults to 1.0.No
repetition_penaltyfloatThe parameter for repetition penalty. 1.0 means no penalty. See this paper (opens in a new tab) for more details. Defaults to 1.0.No

Example

{
  "prompt": "This is a sample prompt",
  "pattern": ".*prompt$",
  "max_new_tokens": 10,
  "stop_after_match": false,
  "top_k": 50,
  "top_p": 0.9,
  "temperature": 0.8,
  "repetition_penalty": 1.2
}

Response

The response from the server includes the completion data which matches the given patterns.

If there was a problem with the request, you will receive an error message instead.

Fields

FieldTypeDescription
completionstringThe generated text that matches the pattern.
tokens_generatedintegerNumber of tokens generated in the response.
stop_reasonstringThe reason the generation was stopped, if applicable. Either pattern_match or token_limit.

Example

{
  "completion": "sample response matching prompt",
  "tokens_generated": 5,
  "stop_reason": "pattern_match"
}

Error Handling

In case of a bad request or authentication error, you will receive an HTTP status code other than 200, and a descriptive error message.

{
  "error": "Invalid API Key provided.",
  "code": 401
}