forked from microsoft/promptflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintent.py
73 lines (59 loc) · 2.52 KB
/
intent.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
import os
import pip
from langchain.chat_models import AzureChatOpenAI
from langchain.prompts.chat import ChatPromptTemplate, HumanMessagePromptTemplate
from langchain.prompts.prompt import PromptTemplate
from langchain.schema import HumanMessage
def extract_intent(chat_prompt: str):
if "AZURE_OPENAI_API_KEY" not in os.environ:
# load environment variables from .env file
try:
from dotenv import load_dotenv
except ImportError:
# This can be removed if user using custom image.
pip.main(["install", "python-dotenv"])
from dotenv import load_dotenv
load_dotenv()
chat = AzureChatOpenAI(
deployment_name=os.environ["CHAT_DEPLOYMENT_NAME"],
openai_api_key=os.environ["AZURE_OPENAI_API_KEY"],
openai_api_base=os.environ["AZURE_OPENAI_API_BASE"],
openai_api_type="azure",
openai_api_version="2023-07-01-preview",
temperature=0,
)
reply_message = chat([HumanMessage(content=chat_prompt)])
return reply_message.content
def generate_prompt(customer_info: str, history: list, user_prompt_template: str):
chat_history_text = "\n".join(
[message["role"] + ": " + message["content"] for message in history]
)
prompt_template = PromptTemplate.from_template(user_prompt_template)
chat_prompt_template = ChatPromptTemplate.from_messages(
[
HumanMessagePromptTemplate(prompt=prompt_template)
]
)
return chat_prompt_template.format_prompt(customer_info=customer_info, chat_history=chat_history_text).to_string()
if __name__ == "__main__":
import json
with open("./data/denormalized-flat.jsonl", "r") as f:
data = [json.loads(line) for line in f.readlines()]
# only ten samples
data = data[:10]
# load template from file
with open("user_intent_zero_shot.md", "r") as f:
user_prompt_template = f.read()
# each test
for item in data:
chat_prompt = generate_prompt(item["customer_info"], item["history"], user_prompt_template)
reply = extract_intent(chat_prompt)
print("=====================================")
# print("Customer info: ", item["customer_info"])
# print("+++++++++++++++++++++++++++++++++++++")
print("Chat history: ", item["history"])
print("+++++++++++++++++++++++++++++++++++++")
print(reply)
print("+++++++++++++++++++++++++++++++++++++")
print(f"Ground Truth: {item['intent']}")
print("=====================================")