MCPcopy Create free account
hub / github.com/EZ-hwh/AutoScraper / request_parse

Method request_parse

module/stepback_crawler.py:105–136  ·  view source on GitHub ↗

A safe and reliable call to LLMs, which confirm that the output can be parsed by json.loads(). Args: query (str): the query to prompt the LLM html (str): the HTML text for Returns: str: a dict parsed from the output of LLM

(self, 
                      query: str,
                      keys: list[str] = [])

Source from the content-addressed store, hash-verified

103 self.error_max_times = error_max_times
104
105 def request_parse(self,
106 query: str,
107 keys: list[str] = []) -> dict[str, str]:
108 """A safe and reliable call to LLMs, which confirm that the output can be parsed by json.loads().
109
110 Args:
111 query (str): the query to prompt the LLM
112 html (str): the HTML text for
113
114 Returns:
115 str: a dict parsed from the output of LLM
116 """
117 pattern = r'\{.*?\}'
118 target = False
119 for _ in range(self.error_max_times):
120 response = self.api(query)
121 matches = re.findall(pattern, response, re.DOTALL)
122 try:
123 for match in matches:
124 res = json.loads(match) # type: ignore
125 for key in keys:
126 assert res[key]
127 target = True
128 if target:
129 break
130 except:
131 pass
132 if target:
133 #print(res)
134 return res
135 else:
136 return {key:"" for key in keys}
137
138 def generate_sequence_html(self,
139 instruction: str,

Callers 4

generate_sequenceMethod · 0.95
rule_synthesisMethod · 0.95
rule_synthesis_culMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected