Created
January 18, 2024 01:27
-
-
Save jcanizalez/e089e3ab8eaf119f8ee622cfa364ed8c to your computer and use it in GitHub Desktop.
Prometheus Langchain Agent
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 langchain.tools import BaseTool | |
| from typing import Optional | |
| from pydantic import BaseModel, Field | |
| from langchain_openai.chat_models import ChatOpenAI | |
| from langchain.agents import initialize_agent | |
| import requests | |
| class PrometheusQueryToolConfig(BaseModel): | |
| prometheus_url: str = Field(default="http://localhost:9090") | |
| class PrometheusQueryTool(BaseTool): | |
| name = "Prometheus Query" | |
| description = "Tool for querying a Prometheus server" | |
| config: Optional[PrometheusQueryToolConfig] = None | |
| def __init__(self, prometheus_url: str = "http://localhost:9090"): | |
| super().__init__() | |
| self.config = PrometheusQueryToolConfig(prometheus_url=prometheus_url) | |
| def _run(self, query: str): | |
| params = {'query': query} | |
| response = requests.get(f"{self.config.prometheus_url}/api/v1/query", params=params) | |
| if response.status_code == 200: | |
| return response.json() | |
| else: | |
| return f"Error: {response.text}" | |
| def _arun(self, query: str): | |
| raise NotImplementedError("This tool does not support async") | |
| # Initialize the Prometheus Query Tool with the URL of your Prometheus server | |
| prometheus_tool = PrometheusQueryTool(prometheus_url="http://localhost:9090") | |
| # Initialize LLM (ChatOpenAI) | |
| llm = ChatOpenAI(temperature=0, model_name='gpt-4') | |
| # Include the Prometheus tool in the list of tools | |
| tools = [prometheus_tool] | |
| # Prompt for the agent | |
| prompt = """ | |
| Use metrics to answer this question: | |
| {text} | |
| """ | |
| # Initialize agent with tools | |
| agent = initialize_agent( | |
| tools=tools, | |
| llm=llm, | |
| verbose=True, | |
| ) | |
| # Example query to the agent | |
| response = agent(prompt.format(text="Can you check the status for all the pods in default namespace?")) | |
| print(response) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I have tried this approach to get the data out of my prometheus, its forming the query but not returning the results properly
Action: Prometheus Query
Action Input: query=up{job="PT-dse"}
Observation Sure! I can use Prometheus to check if the servers are up for PT-dse.
Action: Prometheus Query
Action Input: query=up{job="PT-dse"}
Observation
Observation: Error: {"status":"error","errorType":"bad_data","error":"invalid parameter 'query': 1:6: parse error: unexpected "=""}
Thought: Thought: Hmm, it looks like there's a problem with my query. Let me check the Prometheus documentation to see if I can figure out what's wrong.