-
Notifications
You must be signed in to change notification settings - Fork 5k
/
Copy pathrun_text_llm.py
73 lines (58 loc) · 2.46 KB
/
run_text_llm.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
def run_text_llm(llm, params):
## Setup
if llm.execution_instructions:
try:
# Add the system message
params["messages"][0]["content"] += "\n" + llm.execution_instructions
except:
print('params["messages"][0]', params["messages"][0])
raise
## Convert output to LMC format
inside_code_block = False
accumulated_block = ""
language = None
for chunk in llm.completions(**params):
if llm.interpreter.verbose:
print("Chunk in coding_llm", chunk)
if "choices" not in chunk or len(chunk["choices"]) == 0:
# This happens sometimes
continue
content = chunk["choices"][0]["delta"].get("content", "")
if content == None:
continue
accumulated_block += content
if accumulated_block.endswith("`"):
# We might be writing "```" one token at a time.
continue
# Did we just enter a code block?
if "```" in accumulated_block and not inside_code_block:
inside_code_block = True
accumulated_block = accumulated_block.split("```")[1]
# Did we just exit a code block?
if inside_code_block and "```" in accumulated_block:
return
# If we're in a code block,
if inside_code_block:
# If we don't have a `language`, find it
if language is None and "\n" in accumulated_block:
language = accumulated_block.split("\n")[0]
# Default to python if not specified
if language == "":
if llm.interpreter.os == False:
language = "python"
elif llm.interpreter.os == False:
# OS mode does this frequently. Takes notes with markdown code blocks
language = "text"
else:
# Removes hallucinations containing spaces or non letters.
language = "".join(char for char in language if char.isalpha())
# If we do have a `language`, send it out
if language:
yield {
"type": "code",
"format": language,
"content": content.replace(language, ""),
}
# If we're not in a code block, send the output as a message
if not inside_code_block:
yield {"type": "message", "content": content}