Created
April 2, 2025 00:31
-
-
Save claytantor/a94a86ff669a4fb3adbe3eb562dca56a to your computer and use it in GitHub Desktop.
LM Studio and Pydantic AI in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from typing import Sequence | |
| import requests | |
| import os | |
| from pydantic import BaseModel, Field, ValidationError | |
| import json | |
| # load the environment file with py-dotenv | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| from openai import OpenAI | |
| import lmstudio as lms | |
| client = lms.Client(api_host="simbi.local:1234") | |
| # [LLM(identifier='mistralai_mistral-small-3.1-24b-instruct-2503')] | |
| models:Sequence[lms.LLM] = client.list_loaded_models() | |
| mistral:lms.LLM = None | |
| for model in models: | |
| model: lms.LLM | |
| if model.identifier == "mistralai_mistral-small-3.1-24b-instruct-2503": | |
| mistral = model | |
| break | |
| assert mistral is not None, "Mistral model not found" | |
| class NewsArticle(BaseModel): | |
| title: str = Field(..., description="The title of the news article") | |
| description: str = Field(..., description="The description of the news article") | |
| class AnalysisResult(BaseModel): | |
| input_text: str = Field(..., description="The input text sent for analysis") | |
| analysis: str = Field(..., description="The analysis result from OpenAI") | |
| # Function to analyze a news article using the custom OpenAI client | |
| def analyze_with_lm_studio(article: NewsArticle) -> AnalysisResult: | |
| # Craft the prompt to instruct the model on the output format. | |
| prompt = \ | |
| f""" | |
| Analyze the following news article and provide a JSON object with the following keys: | |
| summary: a short summary of the article | |
| sentiment: the overall sentiment of the article (e.g., positive, negative, neutral) | |
| - key_points: a list of the main points discussed in the article | |
| News article: {article.description} | |
| """ | |
| try: | |
| response:lms.PredictionResult = mistral.complete(prompt, config={"maxTokens": 100}) | |
| analysis = response.content.strip() | |
| return AnalysisResult(input_text=article.description, analysis=analysis) | |
| except Exception as e: | |
| print(f"Error analyzing with LM Studio: {e}") | |
| return None | |
| # Function to fetch latest news stories from an API | |
| def get_latest_news(api_url): | |
| try: | |
| response = requests.get(api_url) | |
| response.raise_for_status() # Raise an error for bad responses | |
| return response.json() | |
| except requests.exceptions.RequestException as e: | |
| print(f"Error fetching news: {e}") | |
| return None | |
| # Function to process news using LM Studio and pydantic AI | |
| def process_news(news_data): | |
| # Here you would integrate with LM Studio and pydantic AI | |
| # This is a placeholder for the actual integration code | |
| if not news_data: | |
| print("No news data to process.") | |
| return | |
| for article in news_data['articles']: | |
| title = article.get('title', 'No Title') | |
| description = article.get('description', 'No Description') | |
| print(f"Processing article: {title}, {description}") | |
| if not title or not description: | |
| print("Skipping article with missing title or description.") | |
| continue | |
| article = NewsArticle(title=title, description=description) | |
| analysis_result = analyze_with_lm_studio(article) | |
| if analysis_result: | |
| print(f"Title: {title}") | |
| print(f"Description: {description}") | |
| print(f"Analysis: {analysis_result.analysis}\n") | |
| else: | |
| print(f"Failed to analyze article: {title}\n") | |
| # Define the Pydantic model for the analysis result. | |
| class ArticleAnalysis(BaseModel): | |
| summary: str | |
| sentiment: str | |
| key_points: list[str] | |
| # Main function to run the example | |
| def main(): | |
| api_key = os.getenv("NEWS_API_KEY") | |
| if not api_key: | |
| print("Please set the NEWS_API_KEY environment variable.") | |
| return | |
| api_url = f'https://newsapi.org/v2/top-headlines?country=us&apiKey={os.getenv("NEWS_API_KEY")}' | |
| news_data = get_latest_news(api_url) | |
| process_news(news_data) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment