[811e40]: / src / prompt.py

Download this file

53 lines (41 with data), 1.6 kB

 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
from typing import Dict, List
from dotenv import load_dotenv
import os
from langchain.prompts.few_shot import FewShotPromptTemplate
from langchain.prompts import PromptTemplate
from langchain.output_parsers import CommaSeparatedListOutputParser
from langchain_openai import ChatOpenAI
load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
openai = ChatOpenAI(
model_name="gpt-3.5-turbo",
openai_api_key=OPENAI_API_KEY,
temperature=0.0,
)
def few_shot_entity_recognition(
examples: List[Dict], criterion: str, entity: str
) -> List[str]:
"""Returns the predicted entities for a given criterion
Args:
examples (List[Dict]): List of few-shot examples
criterion (str): Eligibility criterion
entity (str): Entity type
Returns:
List[str]: List of predicted entities
"""
output_parser = CommaSeparatedListOutputParser()
format_instructions = output_parser.get_format_instructions()
example_prompt = PromptTemplate(
input_variables=["criterion", "entities"],
template="criterion: {criterion} \n entities: {entities}",
)
few_shot_prompt = FewShotPromptTemplate(
examples=examples,
example_prompt=example_prompt,
prefix=f"""Find examples of {entity} in the following criterion. {format_instructions}
If no examples are found, type 'None'. Return only the entities found in the criterion. \n""",
suffix="criterion: {criterion} \n entities:",
input_variables=["criterion"],
)
chain = few_shot_prompt | openai | output_parser
return chain.invoke({"criterion": criterion})