Skip to content

Commit

Permalink
Fix intent-copilot Sample all line returns same results (microsoft#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
lalala123123 authored Aug 24, 2023
1 parent 3e937a1 commit ff21971
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ additional_includes:
### 2. Test & run the flow with additional includes
In this sample, this flow will references some files in the [web-classification](../web-classification/README.md) flow.
You can execute this flow with additional_include locally or submit it to cloud.
You can execute this flow with additional_include locally or submit it to cloud. The snapshot generated by Promptflow contains additional include files/folders.
#### Test flow with single line data
Expand All @@ -60,7 +60,6 @@ pf run create --flow . --data ./data.jsonl --stream
# create run using yaml file
pf run create --file run.yml --stream
```
Note: the snapshot folder in run should contain the additional_includes file.

#### Submit run to cloud

Expand All @@ -72,4 +71,4 @@ Note: the snapshot folder in run should contain the additional_includes file.
# pfazure run create --file run.yml --stream # serverless compute
```

Note: the snapshot folder in run should contain the additional_includes file. Click portal_url of the run to view the final snapshot.
Note: Click portal_url of the run to view the final snapshot.
14 changes: 2 additions & 12 deletions examples/flows/standard/intent-copilot/.promptflow/flow.tools.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"package": {},
"code": {
"user_prompt_template": {
"chat_prompt": {
"type": "prompt",
"inputs": {
"customer_info": {
Expand All @@ -20,17 +20,7 @@
"extract_intent_tool.py": {
"type": "python",
"inputs": {
"customer_info": {
"type": [
"string"
]
},
"history": {
"type": [
"list"
]
},
"user_prompt_template": {
"chat_prompt": {
"type": [
"string"
]
Expand Down
7 changes: 5 additions & 2 deletions examples/flows/standard/intent-copilot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ cat .env

1. init flow directory - create promptflow folder from existing python file
```bash
pf flow init --flow . --entry intent.py --function extract_intent --prompt-template user_prompt_template=user_intent_zero_shot.jinja2
pf flow init --flow . --entry intent.py --function extract_intent --prompt-template chat_prompt=user_intent_zero_shot.jinja2
```
TODO introduce the generated files
The generated files:
- extract_intent_tool.py: Wrap the func `extract_intent` in the `intent.py` script into a [Python Tool](https://promptflow.azurewebsites.net/tools-reference/python-tool.html).
- flow.dag.yaml: Describes the DAG(Directed Acyclic Graph) of this flow.
- .gitignore: File/folder in the flow to be ignored.

2. create needed custom connection
```bash
Expand Down
11 changes: 2 additions & 9 deletions examples/flows/standard/intent-copilot/extract_intent_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,13 @@


@tool
def extract_intent_tool(
customer_info,
history,
user_prompt_template,
connection: CustomConnection
) -> str:
def extract_intent_tool(chat_prompt, connection: CustomConnection) -> str:

# set environment variables
for key, value in connection.items():
os.environ[key] = value

# call the entry function
return extract_intent(
customer_info=customer_info,
history=history,
user_prompt_template=user_prompt_template,
chat_prompt=chat_prompt,
)
15 changes: 7 additions & 8 deletions examples/flows/standard/intent-copilot/flow.dag.yaml
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
$schema: https://azuremlschemas.azureedge.net/promptflow/latest/Flow.schema.json
inputs:
history:
type: string
customer_info:
type: string
history:
type: list
outputs:
output:
type: string
reference: ${extract_intent.output}
nodes:
- name: user_prompt_template
- name: chat_prompt
type: prompt
source:
type: code
path: user_intent_zero_shot.jinja2
inputs: {} # Please enter the inputs of this node
inputs: # Please check the generated prompt inputs
history: ${inputs.history}
customer_info: ${inputs.customer_info}
- name: extract_intent
type: python
source:
type: code
path: extract_intent_tool.py
inputs:
customer_info: ${inputs.customer_info}
history: ${inputs.history}
user_prompt_template: ${user_prompt_template.output}
chat_prompt: ${chat_prompt.output}
connection: custom_connection
environment:
python_requirements_txt: requirements.txt
34 changes: 19 additions & 15 deletions examples/flows/standard/intent-copilot/intent.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import os
import pip
from langchain import LLMChain
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(customer_info: str, history: list, user_prompt_template: str):
def extract_intent(chat_prompt: str):
if "AZURE_OPENAI_API_KEY" not in os.environ:
# load environment variables from .env file
try:
Expand All @@ -17,10 +18,6 @@ def extract_intent(customer_info: str, history: list, user_prompt_template: str)

load_dotenv()

chat_history_text = "\n".join(
[message["role"] + ": " + message["content"] for message in history]
)

chat = AzureChatOpenAI(
deployment_name=os.environ["CHAT_DEPLOYMENT_NAME"],
openai_api_key=os.environ["AZURE_OPENAI_API_KEY"],
Expand All @@ -29,15 +26,23 @@ def extract_intent(customer_info: str, history: list, user_prompt_template: str)
openai_api_version="2023-07-01-preview",
temperature=0,
)
reply_message = chat([HumanMessage(content=chat_prompt)])
return reply_message.content

chat_prompt_template = ChatPromptTemplate.from_messages(
[HumanMessagePromptTemplate.from_template(user_prompt_template)]
)

chain = LLMChain(llm=chat, prompt=chat_prompt_template)
def generate_prompt(customer_info: str, history: list, user_prompt_template: str):

reply = chain.run(customer_info=customer_info, chat_history=chat_history_text)
return reply
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__":
Expand All @@ -55,9 +60,8 @@ def extract_intent(customer_info: str, history: list, user_prompt_template: str)

# each test
for item in data:
reply = extract_intent(
item["customer_info"], item["history"], user_prompt_template
)
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("+++++++++++++++++++++++++++++++++++++")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
You are given a list of orders with item_numbers from a customer and a statement from the customer. It is your job to identify

the intent that the customer has with their statement. Possible intents can be:

"product return", "product exchange", "general question", "product question", "other".




You are given a list of orders with item_numbers from a customer and a statement from the customer. It is your job to identify the intent that the customer has with their statement. Possible intents can be: "product return", "product exchange", "general question", "product question", "other".

In triple backticks below is the customer information and a list of orders.

Expand All @@ -16,20 +8,14 @@ In triple backticks below is the customer information and a list of orders.

```




In triple backticks below are the is the chat history with customer statements and replies from the customer service agent:

```

{{chat_history}}
{{history}}

```




What is the customer's `intent:` here?

"product return", "exchange product", "general question", "product question" or "other"?
Expand Down
2 changes: 1 addition & 1 deletion examples/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# remove when we publish to pypi
--extra-index-url https://azuremlsdktestpypi.azureedge.net/promptflow/
promptflow[azure]==0.0.102528530
promptflow[azure]==0.0.102612974
promptflow-tools==0.1.0.b4
python-dotenv
langchain
Expand Down

0 comments on commit ff21971

Please sign in to comment.