Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain import OpenAI, Wikipedia
|
2 |
+
from langchain.agents import initialize_agent, Tool
|
3 |
+
|
4 |
+
import os
|
5 |
+
os.environ["OPENAI_API_KEY"] = os.environ.get("open_ai_key") #openai key
|
6 |
+
|
7 |
+
import pickle
|
8 |
+
|
9 |
+
"""# Model Implementation"""
|
10 |
+
|
11 |
+
import_folder_name = "./embedded_kubernetes_docs"
|
12 |
+
|
13 |
+
with open(import_folder_name + '.pkl', 'rb') as f:
|
14 |
+
store = pickle.load(f)
|
15 |
+
|
16 |
+
from typing import Union
|
17 |
+
|
18 |
+
from langchain.docstore.base import Docstore
|
19 |
+
from langchain.docstore.document import Document
|
20 |
+
|
21 |
+
|
22 |
+
class CustomData(Docstore):
|
23 |
+
"""Wrapper around embedded custom data"""
|
24 |
+
datastore = None
|
25 |
+
|
26 |
+
def __init__(self, store) -> None:
|
27 |
+
"""Check that embedded custom data is available."""
|
28 |
+
print(store)
|
29 |
+
self.datastore = store
|
30 |
+
print("initialized")
|
31 |
+
|
32 |
+
def search(self, search: str) -> Union[str, Document]:
|
33 |
+
"""Try to search for wiki page.
|
34 |
+
|
35 |
+
If page exists, return the page summary, and a PageWithLookups object.
|
36 |
+
If page does not exist, return similar entries.
|
37 |
+
|
38 |
+
Try to search for embedded data.
|
39 |
+
If doc page exists, return the first one.
|
40 |
+
|
41 |
+
"""
|
42 |
+
docs = self.datastore.similarity_search(search)
|
43 |
+
# print(docs[0].page_content)
|
44 |
+
return docs[0].page_content
|
45 |
+
# try:
|
46 |
+
|
47 |
+
# except wikipedia.PageError:
|
48 |
+
# result = f"Could not find [{search}]. Similar: {wikipedia.search(search)}"
|
49 |
+
# except wikipedia.DisambiguationError:
|
50 |
+
# result = f"Could not find [{search}]. Similar: {wikipedia.search(search)}"
|
51 |
+
# return result
|
52 |
+
|
53 |
+
from typing import Any, List, Optional, Tuple
|
54 |
+
from langchain.docstore.base import Docstore
|
55 |
+
from langchain.docstore.document import Document
|
56 |
+
|
57 |
+
class DocstoreExplorer:
|
58 |
+
"""Class to assist with exploration of a document store."""
|
59 |
+
def __init__(self, docstore: Docstore):
|
60 |
+
"""Initialize with a docstore, and set initial document to None."""
|
61 |
+
self.docstore = docstore
|
62 |
+
self.document: Optional[Document] = None
|
63 |
+
self.llm = OpenAI(temperature=0.7)
|
64 |
+
self.prompt = "You are an expert at Kubernetes. Summarize the following input: "
|
65 |
+
|
66 |
+
def summarize (self, result: Document) -> str:
|
67 |
+
text = self.prompt + result
|
68 |
+
return self.llm(text)
|
69 |
+
|
70 |
+
def search(self, term: str) -> str:
|
71 |
+
"""Search for a term in the docstore, and if found save."""
|
72 |
+
result = self.docstore.search(term)
|
73 |
+
summary = self.summarize(result)
|
74 |
+
print("summary: ", summary)
|
75 |
+
if isinstance(result, Document):
|
76 |
+
self.document = result
|
77 |
+
return summary # REPLACE THIS by having an LLM run a summarize on this based on the fact that it's an expert programmer.
|
78 |
+
else:
|
79 |
+
self.document = None
|
80 |
+
return summary
|
81 |
+
|
82 |
+
def lookup(self, term: str) -> str:
|
83 |
+
"""Lookup a term in document (if saved)."""
|
84 |
+
if self.document is None:
|
85 |
+
raise ValueError("Cannot lookup without a successful search first")
|
86 |
+
return self.document.lookup(term)
|
87 |
+
|
88 |
+
docstore=DocstoreExplorer(CustomData(store))
|
89 |
+
tools = [
|
90 |
+
Tool(
|
91 |
+
name="Search",
|
92 |
+
func=docstore.search
|
93 |
+
),
|
94 |
+
Tool(
|
95 |
+
name="Lookup",
|
96 |
+
func=docstore.search
|
97 |
+
)
|
98 |
+
]
|
99 |
+
|
100 |
+
llm = OpenAI(temperature=0, model_name="text-davinci-003")
|
101 |
+
react = initialize_agent(tools, llm, agent="react-docstore", verbose=True, return_intermediate_steps=True)
|
102 |
+
|
103 |
+
question = "What kubernetes command can i run to see what's happening in my pod?"
|
104 |
+
response = react({"input":question})
|
105 |
+
|
106 |
+
"""# Gradio Implementation"""
|
107 |
+
|
108 |
+
clerkieExamples=["What kubernetes command can i run to see what's happening in my pod", "How can I create a Secret object in Kubernetes?"]
|
109 |
+
|
110 |
+
import random
|
111 |
+
import gradio as gr
|
112 |
+
import openai
|
113 |
+
import re
|
114 |
+
|
115 |
+
chat_variables = {
|
116 |
+
"Context": "",
|
117 |
+
"StackTrace": "",
|
118 |
+
"isLanguage": "",
|
119 |
+
}
|
120 |
+
def chat(message, history):
|
121 |
+
print(message)
|
122 |
+
history = history or []
|
123 |
+
print("len(history: ", len(history))
|
124 |
+
response = react({"input":message})
|
125 |
+
history.append((message, response['output']))
|
126 |
+
return history, history
|
127 |
+
|
128 |
+
def set_text(inp):
|
129 |
+
return inp
|
130 |
+
|
131 |
+
def clear(arg):
|
132 |
+
return ""
|
133 |
+
|
134 |
+
with gr.Blocks() as demo:
|
135 |
+
user_state=gr.State([])
|
136 |
+
gr.Markdown("""# Welcome to Kuber-Clerkie π€""")
|
137 |
+
gr.Markdown("""Kuber-Clerkie is finetuned on Kubernetes documentation to help you debug your complex Kubernetes errors / answer questions. Please feel free to give it a try and let us know what you think!""")
|
138 |
+
gr.Markdown("""### π P.S. [Check out our GPT-3 based Chrome Extension that debugs your code](https://chrome.google.com/webstore/detail/clerkie-ai/oenpmifpfnikheaolfpabffojfjakfnn) π₯π₯π₯""")
|
139 |
+
with gr.Row():
|
140 |
+
with gr.Column():
|
141 |
+
output = gr.Chatbot().style(color_map=("green", "pink"))
|
142 |
+
# allow_flagging="never"
|
143 |
+
inp = gr.Textbox(placeholder="enter your question here")
|
144 |
+
print(type(inp))
|
145 |
+
btn = gr.Button("Enter message")
|
146 |
+
inp.submit(chat, [inp, user_state], [output, user_state])
|
147 |
+
inp.submit(clear, inp, inp)
|
148 |
+
btn.click(chat, [inp, user_state], [output, user_state])
|
149 |
+
btn.click(clear, inp, inp)
|
150 |
+
gr.Markdown("""### need help? got feedback? have thoughts? etc. β Join the [Discord](https://discord.gg/KvG3azf39U)""")
|
151 |
+
gr.Examples(clerkieExamples,
|
152 |
+
inputs=inp,
|
153 |
+
cache_examples=False,
|
154 |
+
)
|
155 |
+
if __name__ == "__main__":
|
156 |
+
demo.launch(debug=True, share=True)
|