Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Python Code samples related to LLMs


Python Code samples related to LLMs


Here are some Python code samples related to Large Language Models (LLMs):

1. Using LLMs as Virtual Assistants for Python Development

python
import requests

class Weather:
    def __init__(self, api_key):
        self.api_key = api_key
        self.temperature = None

    def get_weather(self, city):
        url = 'https://api.openweathermap.org/data/2.5/weather'
        params = {
            'q': city,
            'appid': self.api_key,
            'units': 'imperial'  # Request temperature in Fahrenheit.
        }
        response = requests.get(url, params=params)
        data = response.json()
        if response.status_code == 200:
            self.temperature = data['main']['temp']
            print(f"The temperature in {city} is {self.temperature}°F.")
        else:
            print(f"Error: {data['message']}")

api_key = "YOUR_API_KEY"
weather = Weather(api_key)
weather.get_weather('London')

2. Practical Prompt Engineering Example

python
# Example of using a Python script to test prompt engineering techniques
# This script can be repurposed for other LLM-assisted tasks

# Example of running the script with different data
python app.py testing-chats.txt

3. Scikit-LLM Example for Text Analysis

python
# Example of using Scikit-LLM to integrate LLMs into the scikit-learn framework
# This example demonstrates how to use advanced language models like ChatGPT for text classification and other text analysis tasks

# Example of using Scikit-LLM for sentiment analysis
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline

# Load the iris dataset
iris = load_iris()
X = iris.data[:, :2]  # we only take the first two features.
y = iris.target

# Split the data into a training set and a test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Create a pipeline with a standard scaler and a logistic regression model
pipeline = Pipeline([
    ('scaler', StandardScaler()),
    ('logreg', LogisticRegression())
])

# Train the pipeline
pipeline.fit(X_train, y_train)

# Evaluate the pipeline
y_pred = pipeline.predict(X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))

4. LLM Chain Example

python
# Example of using LangChain to create dynamic prompts and build LLM Chains with OpenAI models (ChatGPT)
# This example demonstrates how to unlock advanced NLP applications using LLM Chains

# Step 1: Define the prompt template
prompt_template = "Explain the concept of machine learning in one paragraph."

# Step 2: Format the prompt template with user input
user_input = "Machine Learning"
prompt = prompt_template.format(user_input)

# Step 3: Instantiate the LLMChain
llm_chain = LangChain(prompt_template, OpenAI(temperature=0.5))

# Step 4: Execute the LLMChain
response = llm_chain.run(prompt)

# Step 5: Print the response
print(response)

5. Fine-tuning LLMs Example

python
# Example of fine-tuning an existing LLM for a particular use case
# This example demonstrates how to fine-tune an LLM using the LoRA technique

# Step 1: Load the base model
model = DistilBERTForSequenceClassification.from_pretrained('distilbert-base-uncased')

# Step 2: Prepare the data
train_dataset = load_dataset('imdb', split='train')
test_dataset = load_dataset('imdb', split='test')

# Step 3: Define the evaluation metric
eval_metric = accuracy

# Step 4: Fine-tune the model
trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=train_dataset,
    eval_dataset=test_dataset,
    compute_metrics=compute_metrics
)

trainer.train()

# Step 5: Load the fine-tuned model
fine_tuned_model = trainer.model

These examples demonstrate various ways to integrate LLMs into Python applications, including using them as virtual assistants, fine-tuning them for specific tasks, and leveraging them for text analysis and other NLP tasks[1][2][3][4][5].

Citations:
[1] https://www.toptal.com/openai/create-your-own-ai-assistant
[2] https://realpython.com/practical-prompt-engineering/
[3] https://towardsdatascience.com/scikit-llm-power-up-your-text-analysis-in-python-using-llm-models-within-scikit-learn-framework-e9f101ffb6d4
[4] https://vitalflux.com/llm-chain-openai-chatgpt-python-example/
[5] https://www.youtube.com/watch?v=eC6Hd1hFvos