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: - Open a terminal or command prompt. - Type the following command and press Enter:pip3 install package_nameReplace "package_name" with the actual name of the package you want to install. - If this doesn't work, it's likely that the command has been modified for a different version. Check the documentation or the project website for guidance on how to access PyPi for your specific Python environment and version.

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 an interpreted language that allows for easy development and deployment of web applications and data science projects. It has strong support for multiple programming paradigms, including object-oriented, functional, and procedural programming. Additionally, Python's simplicity and ease of use make it suitable for a wide range of tasks across various industries.

Example

Jinja call:

{{ "Explain its importance." | llm_complete(model="ollama/smollm2:360m", context="We were discussing Python's role in data science.") }}
Result: `In the realm of data science, Python is indispensable for several reasons:

  1. Easy to Learn: Python has a simple syntax and is easy to read and write, which makes it an attractive choice among developers. Its popularity can be attributed to this ease of use.

  2. Frameworks: Libraries like NumPy (for numerical computations), pandas (for data manipulation and analysis), and scikit-learn (for machine learning) are built in Python. These libraries simplify tasks such as data cleaning, visualization, and modeling.

  3. Integration with Other Libraries: Python integrates seamlessly with popular data science tools like R, Julia, and Excel. This facilitates the creation of complex analyses through a unified interface.

  4. Community Support: The Python community is vast and supportive. There are numerous forums, blogs, and documentation available for learning and troubleshooting common issues.

  5. Hands-On Experience: Python offers hands-on experience with data analysis using libraries like Matplotlib and Seaborn (for visualization), which can help build a portfolio of projects that showcase expertise in data science.

In summary, Python's role in data science is crucial because it simplifies the process of working with data, allows easy integration with other tools, facilitates hands-on experience through frameworks and documentation, and provides a robust ecosystem for learning, collaborating, and contributing to projects involving data analysis.`

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 ""