You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Instrument the Python Chatbot application and fetch metrics with Prometheus
1. ChatBot app
import openai
import gradio as gr
import os
import threading
import time
import psutil
from prometheus_client import start_http_server, Counter, Histogram, Gauge
# Start Prometheus client
start_http_server(8000) # Exposes metrics at http://localhost:8000
# Define metrics
REQUESTS = Counter('generate_response_total', 'Total number of generate_response calls')
MEMORY_USAGE = Gauge('application_memory_usage_bytes', 'Memory usage of the application')
CPU_USAGE = Gauge('application_cpu_usage_seconds_total', 'Total CPU time used by the application')
RESPONSE_TIME = Histogram('generate_response_duration_seconds', 'Histogram for the response time of generate_response')
ERRORS = Counter('generate_response_errors_total', 'Total number of errors in generate_response')
# Set your OpenAI API key
openai.api_key = os.environ.get("OPENAI_KEY")
def generate_response(prompt, max_tokens=50):
with RESPONSE_TIME.time(): # Measure response time
try:
response = openai.Completion.create(
engine="text-davinci-002",
prompt=prompt,
max_tokens=max_tokens
)
REQUESTS.inc() # Increment request count
return response.choices[0].text
except Exception as e:
ERRORS.inc() # Increment error count
raise e
def generate_response_with_gradio(prompt, max_tokens=50):
response = generate_response(prompt, max_tokens)
return response
iface = gr.Interface(
flagging_dir="/tmp/flagged",
fn=generate_response_with_gradio,
inputs="text",
outputs="text",
layout="horizontal",
title="Few-Shot Prompting",
description="Enter a text prompt.",
capture_session=True
)
def collect_metrics():
process = psutil.Process(os.getpid())
while True:
MEMORY_USAGE.set(process.memory_info().rss) # RSS: Resident Set Size
CPU_USAGE.set(process.cpu_times().user)
time.sleep(5) # Update metrics every 5 seconds
metrics_thread = threading.Thread(target=collect_metrics)
metrics_thread.start()
if __name__ == '__main__':
iface.launch()