Nelly-43 commited on
Commit
f66245a
·
verified ·
1 Parent(s): f35603b

Create explain.py

Browse files
Files changed (1) hide show
  1. src/stata/explain.py +66 -0
src/stata/explain.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ from src.llm import generate
3
+ from src.prompts import build_stata_prompt
4
+
5
+ def linenums(options):
6
+ i = options.find("lines:")
7
+ if i != -1:
8
+ lines = options[i+6:]
9
+ if lines == "":
10
+ lines = None
11
+ else:
12
+ lines = None
13
+ return lines
14
+
15
+ def linenums(options):
16
+ i = options.find("lines:")
17
+ if i != -1:
18
+ lines = options[i+6:]
19
+ if lines == "":
20
+ lines = None
21
+ else:
22
+ lines = None
23
+ return lines
24
+
25
+ def read_file(dofile, lines=None):
26
+ # print(f"Reading file: {dofile}")
27
+ if dofile is not None and dofile != "":
28
+ try:
29
+ with open(dofile, "r") as f:
30
+ file_lines = f.readlines()
31
+ # print(file_lines)
32
+ except Exception as e:
33
+ print("Error reading do-file: " + str(e))
34
+ sys.exit(1)
35
+
36
+ if lines is not None:
37
+ if "-" in lines:
38
+ lines = lines.split("-")
39
+ start = int(lines[0])
40
+ end = int(lines[1])
41
+
42
+ file_lines = file_lines[start-1:end]
43
+ file_lines = [f.strip('\n') for f in file_lines] # Stata is 1-indexed, adjust to 0-indexed
44
+ return "\n".join(file_lines)
45
+ else:
46
+ file_lines = file_lines[int(lines)-1] # line number as written in Stata
47
+ return file_lines
48
+ else:
49
+ return ""
50
+
51
+ def explain_(subcmd, query, cfg, dofile, opts):
52
+
53
+ dofx = read_file(dofile, opts['lines'])
54
+
55
+ context = build_stata_prompt(query, subcmd, dofx, opts)
56
+ return generate(
57
+ """
58
+ You are an expert in the Stata statistical program.
59
+ Your task is to answer questions about Stata code. Respond as succinctly and briefly as possible.
60
+ Prefer to use bullets if appropriate. Do not lie. Do not make-up information.
61
+ If you do not know the answer, say so. You are speaking to your peers.
62
+ Your response must be professional yet comfortable in tone.""",
63
+ context,
64
+ cfg,
65
+ max_tokens=1024
66
+ )