Description This script is used to extract the content from a Telegram post and analyze it with provoded prompt using LM Studio Server and OpenAI's Chat Completion API.
Usage
- Install and run LM Studio Server with desired model. LM Studio Server Link.
- Install required libraries by running
pip install requests beautifulsoup4 openai
- Set up your API credentials:
open_ai
- set it to"open_ai"
api_base
- set it to the base URL of your local OpenAI API, e.g.,"http://localhost:1234/v1"
api_key
- no need to set this up for local testing
- Run the script by executing the Python file
Code Overview
The code consists of two main parts:
- Telegram post scraping: Uses the
requests
andbeautifulsoup4
libraries to extract the content from a Telegram post. - Analysis generation: Uses OpenAI's Chat Completion API to generate a response based on the provided prompt.
Variables
url
: The URL of the webpage to scrapeheaders
: Custom headers for the HTTP requestdescription_ tags
: A list of meta tags with og:description propertydescription
: The final description string with AI prompt for analysisopenai_api_type
,api_base
, andapi_key
: API credentials
Methods
requests.get()
: Sends an HTTP GET request to the specified URLBeautifulSoup()
: Parses the HTML content of a webpagefind_all()
: Finds all meta tags with og:description propertyget('content')
: Retrieves the content of the first meta tagopenai.ChatCompletion.create()
: Creates a new chat completion request
Error Handling
- If the HTTP request returns a status code other than 200, an error message is printed.
- If no description tags are found, an error message is printed.
Output
The final generated response is printed to the console.