-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
86 lines (69 loc) · 2.78 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from llama_index.llms.ollama import Ollama
from llama_parse import LlamaParse
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, PromptTemplate
from llama_index.core.embeddings import resolve_embed_model
from dotenv import load_dotenv
from llama_index.core.tools import QueryEngineTool, ToolMetadata
from llama_index.core.agent import ReActAgent
from prompts import context
from codeReader import code_reader
from pydantic import BaseModel
from llama_index.core.output_parsers import PydanticOutputParser
from llama_index.core.query_pipeline import QueryPipeline
from prompts import code_parser_template
import ast
import os
load_dotenv()
llm = Ollama(model="mistral", request_timeout=1000000)
parser = LlamaParse(result_type="markdown") # for pdf files
file_extractor = {".pdf": parser}
documents = SimpleDirectoryReader("./data", file_extractor=file_extractor).load_data()
embed_model = resolve_embed_model("local:BAAI/bge-m3")
vector_index = VectorStoreIndex.from_documents(documents, embed_model=embed_model)
query_engine = vector_index.as_query_engine(llm=llm)
tools = [
QueryEngineTool(
query_engine=query_engine,
metadata=ToolMetadata(
name="api_documentation",
description="this gives documentation about code for an API. Use this for reading docs for the API",
),
),
code_reader,
]
code_llm = Ollama(model="codellama", request_timeout=10000)
agent = ReActAgent.from_tools(tools, llm=code_llm, verbose=True, context=context)
class CodeOutput(BaseModel):
code: str
description: str
filename: str
parser = PydanticOutputParser(CodeOutput)
json_prompt_str = parser.format(code_parser_template)
json_prompt_tmpl = PromptTemplate(json_prompt_str)
output_pipeline = QueryPipeline(chain=[json_prompt_tmpl, llm])
while (prompt := input("Enter a prompt(q to quit):")) != "q":
retries = 0
while retries < 3:
try:
result = agent.query(prompt)
next_result = output_pipeline.run(response=result)
cleaned_json = ast.literal_eval(str(next_result).replace("assistant:", ""))
break
except Exception as e:
retries += 1
print("Code generated")
print(cleaned_json["code"])
print("\n\nDescription:", cleaned_json["description"])
import os
filename = cleaned_json["filename"]
# Create the "output" directory if it doesn't exist
output_dir = "output"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
output_path = os.path.join(output_dir, filename)
try:
with open(output_path, "a") as f: # Use "a" to append to the file if it exists
f.write(cleaned_json["code"])
print("Saved File", output_path)
except Exception as e:
print("Error:", e)