├── .gitignore
├── CODE_OF_CONDUCT.md
├── README.md
├── chatgpt_robusta_actions
└── chat_gpt.py
└── pyproject.toml
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea/
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Contributor Covenant Code of Conduct
2 |
3 | ## Our Pledge
4 |
5 | We as members, contributors, and leaders pledge to make participation in our
6 | community a harassment-free experience for everyone, regardless of age, body
7 | size, visible or invisible disability, ethnicity, sex characteristics, gender
8 | identity and expression, level of experience, education, socio-economic status,
9 | nationality, personal appearance, race, religion, or sexual identity
10 | and orientation.
11 |
12 | We pledge to act and interact in ways that contribute to an open, welcoming,
13 | diverse, inclusive, and healthy community.
14 |
15 | ## Our Standards
16 |
17 | Examples of behavior that contributes to a positive environment for our
18 | community include:
19 |
20 | * Demonstrating empathy and kindness toward other people
21 | * Being respectful of differing opinions, viewpoints, and experiences
22 | * Giving and gracefully accepting constructive feedback
23 | * Accepting responsibility and apologizing to those affected by our mistakes,
24 | and learning from the experience
25 | * Focusing on what is best not just for us as individuals, but for the
26 | overall community
27 |
28 | Examples of unacceptable behavior include:
29 |
30 | * The use of sexualized language or imagery, and sexual attention or
31 | advances of any kind
32 | * Trolling, insulting or derogatory comments, and personal or political attacks
33 | * Public or private harassment
34 | * Publishing others' private information, such as a physical or email
35 | address, without their explicit permission
36 | * Other conduct which could reasonably be considered inappropriate in a
37 | professional setting
38 |
39 | ## Enforcement Responsibilities
40 |
41 | Community leaders are responsible for clarifying and enforcing our standards of
42 | acceptable behavior and will take appropriate and fair corrective action in
43 | response to any behavior that they deem inappropriate, threatening, offensive,
44 | or harmful.
45 |
46 | Community leaders have the right and responsibility to remove, edit, or reject
47 | comments, commits, code, wiki edits, issues, and other contributions that are
48 | not aligned to this Code of Conduct, and will communicate reasons for moderation
49 | decisions when appropriate.
50 |
51 | ## Scope
52 |
53 | This Code of Conduct applies within all community spaces, and also applies when
54 | an individual is officially representing the community in public spaces.
55 | Examples of representing our community include using an official e-mail address,
56 | posting via an official social media account, or acting as an appointed
57 | representative at an online or offline event.
58 |
59 | ## Enforcement
60 |
61 | Instances of abusive, harassing, or otherwise unacceptable behavior may be
62 | reported to the community leaders responsible for enforcement at
63 | support@robusta.dev.
64 | All complaints will be reviewed and investigated promptly and fairly.
65 |
66 | All community leaders are obligated to respect the privacy and security of the
67 | reporter of any incident.
68 |
69 | ## Enforcement Guidelines
70 |
71 | Community leaders will follow these Community Impact Guidelines in determining
72 | the consequences for any action they deem in violation of this Code of Conduct:
73 |
74 | ### 1. Correction
75 |
76 | **Community Impact**: Use of inappropriate language or other behavior deemed
77 | unprofessional or unwelcome in the community.
78 |
79 | **Consequence**: A private, written warning from community leaders, providing
80 | clarity around the nature of the violation and an explanation of why the
81 | behavior was inappropriate. A public apology may be requested.
82 |
83 | ### 2. Warning
84 |
85 | **Community Impact**: A violation through a single incident or series
86 | of actions.
87 |
88 | **Consequence**: A warning with consequences for continued behavior. No
89 | interaction with the people involved, including unsolicited interaction with
90 | those enforcing the Code of Conduct, for a specified period of time. This
91 | includes avoiding interactions in community spaces as well as external channels
92 | like social media. Violating these terms may lead to a temporary or
93 | permanent ban.
94 |
95 | ### 3. Temporary Ban
96 |
97 | **Community Impact**: A serious violation of community standards, including
98 | sustained inappropriate behavior.
99 |
100 | **Consequence**: A temporary ban from any sort of interaction or public
101 | communication with the community for a specified period of time. No public or
102 | private interaction with the people involved, including unsolicited interaction
103 | with those enforcing the Code of Conduct, is allowed during this period.
104 | Violating these terms may lead to a permanent ban.
105 |
106 | ### 4. Permanent Ban
107 |
108 | **Community Impact**: Demonstrating a pattern of violation of community
109 | standards, including sustained inappropriate behavior, harassment of an
110 | individual, or aggression toward or disparagement of classes of individuals.
111 |
112 | **Consequence**: A permanent ban from any sort of public interaction within
113 | the community.
114 |
115 | ## Attribution
116 |
117 | This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118 | version 2.0, available at
119 | https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
120 |
121 | Community Impact Guidelines were inspired by [Mozilla's code of conduct
122 | enforcement ladder](https://github.com/mozilla/diversity).
123 |
124 | [homepage]: https://www.contributor-covenant.org
125 |
126 | For answers to common questions about this code of conduct, see the FAQ at
127 | https://www.contributor-covenant.org/faq. Translations are available at
128 | https://www.contributor-covenant.org/translations.
129 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## 🚨The Kubernetes ChatGPT Bot project will be deprecated and replaced by [HolmesGPT](https://github.com/robusta-dev/holmesgpt), our advanced Open Source DevOps assistant. With Holmes you can investigate incidents, triage issues, enrich alerts and much more. Check it out [https://github.com/robusta-dev/holmesgpt](https://github.com/robusta-dev/holmesgpt)
2 |
3 | # Introduction
4 |
5 | A ChatGPT[^1] bot for Kubernetes issues. Ask the AI how to solve your Prometheus alerts, get pithy responses.
6 |
7 | No more solving alerts alone in the darkness - the internet has your back.
8 |
9 |
10 |
11 |
12 |
13 | Please consider upvoting on [Product Hunt](https://www.producthunt.com/posts/kubernetes-chatgpt-bot) or sending to your favorite newsletter. One day, Skynet will remember your kindness and spare you!
14 |
15 | # How it works
16 | Prometheus forwards alerts to the bot using a webhook receiver.
17 |
18 | The bot sends a query to OpenAI, asking it how to fix your alerts.
19 |
20 | You stockpile food in your pantry for the robot uprising.
21 |
22 | The bot is implemented using [Robusta.dev](https://github.com/robusta-dev/robusta), an open source platform for responding to Kubernetes alerts. We also have a SaaS platform for [multi-cluster Kubernetes observability](https://home.robusta.dev/).
23 |
24 | # Prerequisites
25 | * A Slack workspace
26 |
27 | # Setup
28 | 1. [Install Robusta with Helm](https://docs.robusta.dev/master/installation.html)
29 | 2. Load the ChatGPT playbook. Add the following to `generated_values.yaml`:
30 | ```
31 | playbookRepos:
32 | chatgpt_robusta_actions:
33 | url: "https://github.com/robusta-dev/kubernetes-chatgpt-bot.git"
34 |
35 | customPlaybooks:
36 | # Add the 'Ask ChatGPT' button to all Prometheus alerts
37 | - triggers:
38 | - on_prometheus_alert: {}
39 | actions:
40 | - chat_gpt_enricher: {}
41 | ```
42 |
43 | 3. Add your [OpenAI API key](https://beta.openai.com/account/api-keys) to `generated_values.yaml`. Make sure you edit the existing `globalConfig` section, don't add a duplicate section.
44 |
45 | ```
46 | globalConfig:
47 | chat_gpt_token: YOUR KEY GOES HERE
48 | ```
49 |
50 | 4. Do a Helm upgrade to apply the new values: `helm upgrade robusta robusta/robusta --values=generated_values.yaml --set clusterName=`
51 |
52 | 5. [Send your Prometheus alerts to Robusta](https://docs.robusta.dev/master/user-guide/alert-manager.html). Alternatively, just use Robusta's bundled Prometheus stack.
53 |
54 | # Demo
55 | Instead of waiting around for a Prometheus alert, lets cause one.
56 |
57 | 1. Deploy a broken pod that will be stuck in pending state:
58 |
59 | ```
60 | kubectl apply -f https://raw.githubusercontent.com/robusta-dev/kubernetes-demos/main/pending_pods/pending_pod_node_selector.yaml
61 | ```
62 |
63 | 2. Trigger a Prometheus alert immediately, skipping the normal delays:
64 |
65 | ```
66 | robusta playbooks trigger prometheus_alert alert_name=KubePodCrashLooping namespace=default pod_name=example-pod
67 | ```
68 |
69 | An alert will arrive in Slack with a button. Click the button to ask ChatGPT about the alert.
70 |
71 | # Future Improvements
72 | Can ChatGPT give better answers if you feed it pod logs or the output of `kubectl get events`?
73 |
74 | [Robusta](http://robusta.dev) already collects this data and attaches it to Prometheus alerts, so it should be easy to add.
75 |
76 | PRs are welcome!
77 |
78 | # Community
79 | [Share your funniest output and suggest new features on our Slack.](https://home.robusta.dev/slack)
80 |
81 | # Promotional Images
82 | Feel free to use the following image or create your own.
83 |
84 | 
85 |
86 | # More Resources
87 | * [Natan tests ChatGPT on production Kubernetes alerts](https://www.youtube.com/watch?v=RVK6jb4Ssuo)
88 | * [Natan Yellin and Sid Palas go over the code on YouTube](https://www.youtube.com/watch?v=jMR8M3Xqlzg
89 | ) - relevant part starts at 38:54
90 |
91 | [^1]: Technically this project doesn't use ChatGPT. It uses the `text-davinci-003` model which is a [GPT3.5-based sibling of ChatGPT](https://matt-rickard.ghost.io/gpt-lineage/). Given that most people are familiar with ChatGPT, but not `text-davinci-003` or GPT3.5, we've decided to keep the name "ChatGPT bot" despite the technical inaccuracy.
92 |
--------------------------------------------------------------------------------
/chatgpt_robusta_actions/chat_gpt.py:
--------------------------------------------------------------------------------
1 | import logging
2 | import time
3 |
4 | import cachetools
5 | import openai
6 | from openai.openai_object import OpenAIObject
7 | from robusta.api import *
8 |
9 | cache_size = 100
10 | lru_cache = cachetools.LRUCache(maxsize=cache_size)
11 | class ChatGPTTokenParams(ActionParams):
12 | """
13 | :var chat_gpt_token: ChatGPT auth token
14 | """
15 | chat_gpt_token: str
16 |
17 |
18 | class ChatGPTParams(ChatGPTTokenParams):
19 | """
20 | :var search_term: ChatGPT search term
21 | :var model: ChatGPT OpenAi API model
22 | """
23 | search_term: str
24 | model: str = "gpt-3.5-turbo"
25 |
26 |
27 | @action
28 | def show_chat_gpt_search(event: ExecutionBaseEvent, params: ChatGPTParams):
29 | """
30 | Add a finding with ChatGPT top results for the specified search term.
31 | This action can be used together with the stack_overflow_enricher.
32 | """
33 | openai.api_key = params.chat_gpt_token
34 |
35 | logging.info(f"ChatGPT search term: {params.search_term}")
36 |
37 | answers = []
38 | try:
39 | if params.search_term in lru_cache:
40 | answers = lru_cache[params.search_term]
41 | else:
42 | start_time = time.time()
43 | input = [
44 | {"role": "system", "content": "You are a helpful assistant that helps Software Developers and DevOps Engineers to solve issues relating to Prometheus alerts for Kubernetes clusters. You are factual, clear and concise. Your responses are formatted using Slack specific markdown to ensure compatibility with displaying your response in a Slack message"},
45 | {"role": "user", "content": f"Here are the rules for Slack specific markdown, make sure to only use the following syntax in your responses : Text formatted in bold Surround text with asterisks: '*your text*', '**' is invalid syntax so do not use it. Text formatted in italics, surround text with underscores: '_your text_'. Text formatted in strikethrough, surround text with tildes: '~your text~'. Text formatted in code, surround text with backticks: '`your text`'. Text formatted in blockquote, add an angled bracket in front of text: '>your text'. Text formatted in code block, add three backticks in front of text: '```your text'. Text formatted in an ordered list, add 1 and a full stop '1.' in front of text. Text formatted in a bulleted list, add an asterisk in front of text: '* your text'."},
46 | {"role": "user", "content": f"When responding, you use Slack specific markdown following the rules provided. Always bold and italic headings, i.e '*_The heading:_*', to clearly seperate the content with headers. Don't include any conversational response before the facts."},
47 | {"role": "user", "content": f"Please describe what the Kubernetes Prometheus alert '{params.search_term}' means, giving succinct examples of common causes. Provide any possible solutions including any troubleshooting steps that can be performed, give a real world example of a situation that can cause the alert can occur. Clearly seperate sections for Alert Name, Description, Real World Example, Common Causes, Troubleshooting Steps and Possible Solutions."},
48 | ]
49 |
50 | logging.info(f"ChatGPT input: {input}")
51 | res: OpenAIObject = openai.ChatCompletion.create(
52 | model=params.model,
53 | messages=input,
54 | max_tokens=1000,
55 | temperature=0
56 | )
57 | if res:
58 | logging.info(f"ChatGPT response: {res}")
59 | total_tokens = res.usage['total_tokens']
60 | time_taken = time.time() - start_time
61 | response_content = res.choices[0].message.content
62 | lru_cache[params.search_term] = [response_content] # Store only the main response in the cache
63 | answers.append(response_content)
64 |
65 | answers.append(f"\n\n ---")
66 | answers.append(f"\n\n | Time taken: {time_taken:.2f} seconds | Total tokens used: {total_tokens} |")
67 |
68 | except Exception as e:
69 | answers.append(f"Error calling ChatCompletion.create: {e}")
70 | raise
71 |
72 | finding = Finding(
73 | title=f"ChatGPT ({params.model}) Results",
74 | source=FindingSource.PROMETHEUS,
75 | aggregation_key="ChatGPT Wisdom",
76 | )
77 |
78 | if answers:
79 | finding.add_enrichment([MarkdownBlock('\n'.join(answers))])
80 | else:
81 | finding.add_enrichment(
82 | [
83 | MarkdownBlock(
84 | f'Sorry, ChatGPT doesn\'t know anything about "{params.search_term}"'
85 | )
86 | ]
87 | )
88 | event.add_finding(finding)
89 |
90 | @action
91 | def chat_gpt_enricher(alert: PrometheusKubernetesAlert, params: ChatGPTTokenParams):
92 | """
93 | Add a button to the alert - clicking it will ask chat gpt to help find a solution.
94 | """
95 | alert_name = alert.alert.labels.get("alertname", "")
96 | if not alert_name:
97 | return
98 |
99 | alert.add_enrichment(
100 | [
101 | CallbackBlock(
102 | {
103 | f'Ask ChatGPT: {alert_name}': CallbackChoice(
104 | action=show_chat_gpt_search,
105 | action_params=ChatGPTParams(
106 | search_term=f"{alert_name}",
107 | chat_gpt_token=params.chat_gpt_token,
108 | ),
109 | )
110 | },
111 | )
112 | ]
113 | )
114 |
--------------------------------------------------------------------------------
/pyproject.toml:
--------------------------------------------------------------------------------
1 | [tool.poetry]
2 | name = "chatgpt_robusta_actions"
3 | version = "0.0.1"
4 | description = ""
5 | authors = ["Arik Alon "]
6 |
7 | [tool.poetry.dependencies]
8 | openai = "^0.27.2"
9 | cachetools = "^5.3.0"
10 |
11 |
12 | [tool.poetry.dev-dependencies]
13 | robusta-cli = "^0.10.14"
14 |
15 | [build-system]
16 | requires = ["poetry-core>=1.0.0"]
17 | build-backend = "poetry.core.masonry.api"
18 |
--------------------------------------------------------------------------------