Spaces:
Sleeping
Sleeping
File size: 1,189 Bytes
ae80c9a |
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 |
from functools import partial
import yaml
from langchain.chains import LLMChain
from langchain.output_parsers import NumberedListOutputParser
from langchain.prompts import ChatPromptTemplate
with open("./schema.yml") as f:
schema = yaml.safe_load(f)
class AxiomParser(NumberedListOutputParser):
def parse(self, text: str) -> str:
axioms = super().parse(text=text)
return " ".join(axioms)
def get_format_instructions(self) -> str:
return super().get_format_instructions()
class TripletParser(NumberedListOutputParser):
def parse(self, text: str) -> str:
output = super().parse(text=text)
headers = ["subject", "relation", "object"]
triplets = [dict(zip(headers, item.split("::"))) for item in output]
return triplets
def get_format_instructions(self) -> str:
return super().get_format_instructions()
chains = {}
for scheme in schema:
parser = schema[scheme]["parser"]
prompts = schema[scheme]["prompts"]
chains[scheme] = partial(
LLMChain,
output_parser=eval(f'{parser}()'),
prompt=ChatPromptTemplate.from_messages(list(prompts.items()))
) |