-
I want to extract the JSON from the guidance output. I can reconstruct the JSON using the stored variables in In general, the question pertains to extracting a fraction of data that contains both input and LLM-generated output. import guidance
lm = phi2
# define a re-usable "guidance function" that we can use below
@guidance
def quoted_list(lm, name, n):
for i in range(n):
if i > 0:
lm += ", "
lm += '"' + gen(name, list_append=True, stop='"') + '"'
return lm
lm = lm + f"""What are the most common commands used in the Linux operating system?
Here are the 5 most common commands in JSON format:
{{
"commands": [{quoted_list('commands', 5)}],
"my_favorite_command": "{gen('favorite_command', stop='"')}"
}}"""
print(str(lm)) output:
|
Beta Was this translation helpful? Give feedback.
Answered by
hudson-ai
Mar 5, 2024
Replies: 1 comment 3 replies
-
I used such methods to do it: def fill_json(text: str, llm):
prompt = '"... create it for such sentence:"
output = llm + _fill_json(prompt, text)
return json.loads(output['json'])
@guidance
def _fill_json(lm, prompt, text):
lm += prompt + f" {text}\n```json" + gen('json', stop='```')
return lm |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can enclose the entire bit that generates JSON in a guidance
block
:)I.e.