Different LLMs with funcchain EASY TO USE¶
Example
See llamacpp.py Also see supported MODELS.md
In this example, we will use the funcchain library to perform sentiment analysis on a piece of text. This showcases how funcchain can seamlessly utilize different Language Models (LLMs) using local llamacpp models, without many code changes..
This is particularly useful for developers looking to integrate different models in a single application or just experimenting with different models.
Full Code Example¶
from funcchain import chain, settings
from pydantic import BaseModel, Field
from rich import print
# define your model
class SentimentAnalysis(BaseModel):
analysis: str = Field(description="A description of the analysis")
sentiment: bool = Field(description="True for Happy, False for Sad")
# define your prompt
def analyze(text: str) -> SentimentAnalysis:
"""
Determines the sentiment of the text.
"""
return chain()
# set global llm
settings.llm = "llamacpp/openchat-3.5-0106:Q3_K_M"
# log tokens as stream to console
settings.console_stream = True
# run prompt
poem = analyze("I really like when my dog does a trick!")
# show final parsed output
print(poem)
Demo¶
Instructions¶
Step-by-step
Necessary Imports
Define the Data Model
Here, we define a SentimentAnalysis
model with a description of the sentiment analysis and a boolean field indicating the sentiment.
class SentimentAnalysis(BaseModel):
analysis: str = Field(description="A description of the analysis")
sentiment: bool = Field(description="True for Happy, False for Sad")
Create the Analysis Function
This 'analyze' function takes a string as input and is expected to return a SentimentAnalysis
object by calling the chain()
function from the funcchain
library.
def analyze(text: str) -> SentimentAnalysis:
"""
Determines the sentiment of the text.
"""
return chain()
Execution Configuration
In the main block, configure the global settings to set the preferred LLM, enable console streaming, and run the analyze
function with sample text.
# set global llm
settings.llm = "llamacpp/openchat-3.5-0106:Q3_K_M"
# log tokens as stream to console
settings.console_stream = True
# run prompt
poem = analyze("I really like when my dog does a trick!")
# show final parsed output
print(poem)
Important
We need to note here is that settings.llm
can be adjusted to any model mentioned in MODELS.md and your funcchain code will still work and chain()
does everything in the background for you.