2024-01-20 23:07:28 +00:00
#!/home/brickman/anaconda3/bin/python
import requests
import json
import requests
from markdown import markdown
from bs4 import BeautifulSoup
from rich.console import Console
from rich.markdown import Markdown
2024-01-21 00:40:16 +00:00
import sys, os
2024-01-21 01:16:04 +00:00
import tempfile
2024-01-20 23:07:28 +00:00
try:
if len(sys.argv[1]) > 1:
question = sys.argv[1]
else:
print("Error: Please specifice a question")
exit()
except:
print("Error: Please specifice a question")
exit()
def codeparse(text_code):
html = markdown(text_code, output_format="html5")
soup = BeautifulSoup(html, "html.parser")
for code in soup.findAll('code'):
code = str(code)
code = code.replace("<code>bash\n","")
code = code.replace("<code>","")
code = code.replace("</code>","")
print(code)
def check_substring(main_string, substring):
return substring in main_string
def generate_text(model, prompt, system):
url = "http://localhost:11434/api/generate"
data = {
"model": model,
"prompt": prompt,
"system": system,
"stream": False,
"options": {
2024-01-21 00:40:16 +00:00
"temperature": 0.6,
2024-01-20 23:07:28 +00:00
}
}
response = requests.post(url, json=data)
text = json.loads(response.text)
return text["response"]
out = ""
2024-01-21 00:40:16 +00:00
count = 0
2024-01-20 23:07:28 +00:00
while not check_substring(out, "CODETRUE"):
2024-01-21 00:40:16 +00:00
#print('Thinking....')
2024-01-20 23:07:28 +00:00
text = generate_text("codellama:7b", f"You are Linux Assistant. I will ask you a question in natural language and you will return a single bash code block. Make sure that you keep the result as brief as posible.\n\nPrompt:\n{question}", """You are Linux Assistant. I will ask you a question in natural language and you will return a single bash code block. The format should be as follows:
Discription here
```bash
code goes here
```
""")
#text = 'The `sudo` command takes an optional argument `-h` which stands for "help". Running `sudo -h | more` will display the help message for the `sudo` command, which includes a description of its usage and options.'
out = generate_text("codellama:7b", f"""Check the following prompt and see if it lines up with the code.
Look at the prompt and then the resulting bash code. If the result is in the correct format as speciffied below
return CODETRUE if the format is wrong or if the code is incorrect return CODEFALSE. Do not write any code or return CODE_TRUE if you write code that fixes the problem.
Correct format:
Discription here
```bash
code goes here
```
Prompt:
{question}
Bash:
{text}""", "You are a code expert.")
2024-01-21 00:40:16 +00:00
count+=1
if count > 4:
print('\n\nError: Could not find an answer to your problem')
exit()
2024-01-20 23:07:28 +00:00
2024-01-21 00:40:16 +00:00
# Showing llm output
2024-01-20 23:07:28 +00:00
console = Console()
2024-01-21 00:40:16 +00:00
md = Markdown("# LLM Output\n" + text)
2024-01-20 23:07:28 +00:00
console.print(md)
2024-01-21 00:40:16 +00:00
# printing code blocks
html = markdown(text, output_format="html5")
soup = BeautifulSoup(html, "html.parser")
for code in soup.findAll('code'):
code = str(code)
code = code.replace("<code>bash\n","")
code = code.replace("<code>","")
code = code.replace("</code>","")
2024-01-21 01:16:04 +00:00
code = code.replace("&", "&")
2024-01-21 00:40:16 +00:00
if len(code) < 4:
continue
print("\n\n------------------------CODE----------------------------")
console.print(Markdown(f"```bash\n{code}\n```"))
print("--------------------------------------------------------")
2024-01-21 01:51:07 +00:00
answer = input("Would you like to run this code [N/y/e/q]:").lower()
2024-01-21 00:40:16 +00:00
if answer == "y" or answer == "yes":
print("\n")
os.system(code)
2024-01-21 01:16:04 +00:00
print("")
elif answer == "e" or answer == "edit":
file_content = code
t = tempfile.NamedTemporaryFile(delete=False)
t.write(file_content.encode())
t.close()
try:
editor = os.environ['EDITOR']
except KeyError:
editor = 'nano'
os.system(f"{editor} {t.name}")
os.system(f"bash {t.name}")
os.system(f"rm -rf {t.name}")
2024-01-21 01:51:07 +00:00
elif answer == "q" or answer == "quit":
2024-01-21 01:16:04 +00:00
exit()
2024-01-21 01:49:57 +00:00