Skip to content

llm (1)

llm_complete

llm_complete(prompt: str, system_prompt: str | None = None, context: str | None = None, model: str | None = None, token: str | None = None, base_url: str | None = None, **kwargs: Any) -> str

Complete a prompt using the LLM API.

Required packages: litellm

Example

Jinja call:

{{ "Describe how to install a package from PyPi" | llm_complete(model="ollama/smollm2:360m") }}
Result: Here's an easy-to-follow guide on how to install a package from PyPI using the following commands: 1. Open your terminal or command prompt and navigate into the directory where you want to install the package. 2. Run the following command in the terminal (on Linux, macOS, or Windows) depending on your system's package manager:pip install package_name- Example for Python 3.x:python3 -m pip install package_name- Example for Java or other JVM-specific packages:java -jar path/to/package.war --exec /path/to/some/directory`

  1. After you've installed the package, make sure to update your system's package index by running: pip install --upgrade pip
  2. To test that the installation was successful and the package is now available on PyPI, run: python -c "import setuptools; print(setuptools.__version__)"
  3. You can then use Python with the package as usual by importing it in your code: `import package_name``

Example

Jinja call:

{{ "What are the benefits of Python?" | llm_complete(model="ollama/smollm2:360m", system_prompt="You are a helpful programming assistant.") }}
Result: `Python is known for its simplicity and versatility in various fields such as data science, machine learning, web development, automation, and more. Here are some key benefits of using Python:

  1. Easy to learn: Python has a simple syntax and beginner-friendly codebase that makes it an ideal choice for beginners, even those without prior programming experience.
  2. High-performance: Python is known for its performance and efficiency in data processing tasks such as NumPy arrays and Pandas DataFrames, which are essential for big data analysis and machine learning.
  3. Cross-platform compatibility: Python can run on multiple operating systems including Windows, macOS, Linux, and Unix, making it a popular choice among developers working with various environments.
  4. Large community: The Python ecosystem is vast and has numerous libraries and frameworks available for different use cases such as web development, data analysis, artificial intelligence, and more.
  5. Versatile applications: Python can be used in a wide range of applications including automation, games, scientific computing, data science, machine learning, and more.
  6. Extensive libraries and frameworks: Python has an extensive collection of libraries and frameworks that make it easy to develop complex applications quickly. Examples include NumPy, pandas, scikit-learn, and TensorFlow for machine learning, Flask for web development, and more.
  7. Dynamic typing: Python is dynamically typed, which means variables do not need to be explicitly declared with a type before using them. This makes code easier to write and test.
  8. Extensive documentation: The official Python documentation provides comprehensive information on various topics including libraries, modules, and tutorials.
  9. Large number of frameworks and tools: Many popular frameworks such as Django, Flask, and NumPy are built upon top of the standard Python library, making it easier to develop applications quickly.
  10. Open-source and free: Python is open-source software, which means its source code is available for anyone to view and modify. This allows developers to improve the language and contribute to the project without paying a licensing fee.`

Example

Jinja call:

{{ "Explain its importance." | llm_complete(model="ollama/smollm2:360m", context="We were discussing Python's role in data science.") }}
Result: `Python is crucial for working with datasets in data science because of its simplicity and versatility. Here are some reasons why it's so important:

  1. Easy to Learn: Python has a vast number of libraries, frameworks, and tools that make it an excellent choice for data analysis and machine learning tasks. This ease of use allows users to quickly develop complex projects without needing to know multiple programming languages.

  2. Rapid Development: Python's syntax is very concise and flexible, making it ideal for rapid prototyping and development. This is particularly useful in the data science field where projects often require a high degree of speed and agility.

  3. Easy Modularity: Python offers built-in modules (like Pandas, NumPy, and scikit-learn) that allow developers to easily organize their code and reuse it across multiple projects. This modular approach makes it easier to add new features or extend existing ones without starting from scratch each time.

  4. Large Community Support: The Python community is incredibly active, with a vast number of open-source projects and contributions. This means that there are many resources available for learning and troubleshooting Python code, making it easy to find help when needed.

  5. Cross-Language Interoperability: Python's extensive libraries can be easily used across different programming languages. For example, the Pandas library works seamlessly with other data analysis and visualization tools like Matplotlib and Plotly in various languages.

These reasons highlight Python's importance in the field of data science, making it an essential tool for anyone working on big data projects or creating sophisticated data analysis applications.`

DocStrings

Parameters:

Name Type Description Default
prompt str

The prompt to complete.

required
system_prompt str | None

The system prompt to set context for the model.

None
context str | None

Additional context for the prompt.

None
model str | None

The model to use.

None
token str | None

The API token.

None
base_url str | None

The base URL of the API.

None
kwargs Any

Additional keyword arguments passed to litellm.completion.

{}

Returns:

Type Description
str

The completed text from the LLM.

Raises:

Type Description
ValueError

If the API response is invalid or missing expected data.

Source code in src/jinjarope/llmfilters.py
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
def llm_complete(
    prompt: str,
    system_prompt: str | None = None,
    context: str | None = None,
    model: str | None = None,
    token: str | None = None,
    base_url: str | None = None,
    **kwargs: Any,
) -> str:
    """Complete a prompt using the LLM API.

    Args:
        prompt: The prompt to complete.
        system_prompt: The system prompt to set context for the model.
        context: Additional context for the prompt.
        model: The model to use.
        token: The API token.
        base_url: The base URL of the API.
        kwargs: Additional keyword arguments passed to litellm.completion.

    Returns:
        The completed text from the LLM.

    Raises:
        ValueError: If the API response is invalid or missing expected data.
    """
    messages: list[dict[str, str]] = []
    if system_prompt:
        messages.append({"role": "system", "content": system_prompt})
    if context:
        messages.append({"role": "user", "content": context})
    messages.append({"role": "user", "content": prompt})

    response = litellm.completion(
        model=model or os.getenv("OPENAI_MODEL", ""),
        api_key=token or os.getenv("OPENAI_API_TOKEN"),
        api_base=base_url or os.getenv("OPENAI_API_BASE"),
        messages=messages,
        **kwargs,
    )
    if not response.choices or not response.choices[0].message:
        msg = "Invalid API response: missing choices or message"
        raise ValueError(msg)
    return response.choices[0].message.content or ""