Skip to content

Commit

Permalink
tutorial examples (#25)
Browse files Browse the repository at this point in the history
Signed-off-by: Mandana Vaziri <[email protected]>
  • Loading branch information
vazirim authored Sep 9, 2024
1 parent 3a27028 commit 99c7d45
Show file tree
Hide file tree
Showing 20 changed files with 799 additions and 53 deletions.
66 changes: 36 additions & 30 deletions examples/arith/Arith-new.pdl
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,43 @@ document:
- read: examples/arith/example1.txt
- read: examples/arith/example2.txt
- repeat:
- "\nQuestion: "
- def: QUESTION
model: ibm/granite-20b-code-instruct
params:
stop_sequences:
- Answer
include_stop_sequence: false
- "Answer: Let's think step by step.\n"
- repeat:
- def: REASON_OR_CALC
model: ibm/granite-20b-code-instruct
document:
- "\nQuestion: "
- def: QUESTION
model: ibm/granite-34b-code-instruct
params:
stop_sequences:
- '<<'
- Answer
- "?"
include_stop_sequence: true
- if: '{{ REASON_OR_CALC.endswith("<<") }}'
then:
- def: EXPR
model: ibm/granite-20b-code-instruct
params:
stop_sequences:
- '='
- "\n"
- "Question"
include_stop_sequence: false
- '= '
- def: RESULT
lan: python
code: result = {{ EXPR }}
- ' >>'
until: '{{ "The answer is" in REASON_OR_CALC }}'
- "\n\n"
- "\nAnswer: Let's think step by step.\n"
- repeat:
document:
- def: REASON_OR_CALC
model: ibm/granite-34b-code-instruct
params:
stop_sequences:
- '<<'
- "Question"
include_stop_sequence: true
- if: '{{ REASON_OR_CALC.endswith("<<") }}'
then:
document:
- def: EXPR
model: ibm/granite-34b-code-instruct
params:
stop_sequences:
- '='
- "\n"
- "Question"
include_stop_sequence: false
- '= '
- def: RESULT
lan: python
code: result = {{ EXPR }}
- ' >>'
until: '{{ "The answer is" in REASON_OR_CALC }}'
as: document
- "\n\n"
as: document
num_iterations: 3
num_iterations: 3
41 changes: 22 additions & 19 deletions examples/arith/Arith-simple.pdl
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,29 @@ document:
Last month he sold four large boxes and two small boxes.
If he sold half as much this month, how much is his sales for this month?
- repeat:
- def: REASON_OR_CALC
model: ibm/granite-20b-code-instruct
params:
stop_sequences:
- '<<'
include_stop_sequence: true
- if: '{{ REASON_OR_CALC.endswith("<<") }}'
then:
- def: EXPR
document:
- def: REASON_OR_CALC
model: ibm/granite-20b-code-instruct
params:
stop_sequences:
- '='
- "\n"
- "Question"
include_stop_sequence: false
- '= '
- def: RESULT
lan: python
code: result = {{ EXPR }}
- ' >>'
- '<<'
include_stop_sequence: true
- if: '{{ REASON_OR_CALC.endswith("<<") }}'
then:
document:
- def: EXPR
model: ibm/granite-20b-code-instruct
params:
stop_sequences:
- '='
- "\n"
- "Question"
include_stop_sequence: false
- '= '
- def: RESULT
lan: python
code: result = {{ EXPR }}
- ' >>'
as: document
until: '{{ "The answer is" in REASON_OR_CALC }}'
- "\n"
- "\n"
8 changes: 4 additions & 4 deletions examples/fibonacci/fib.pdl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ description: Fibonacci
document:
- def: CODE
model: ibm/granite-20b-code-instruct
input: "Write just the definition for an efficient Python function to compute the Fibonacci sequence\n\n"
input: "Write an optimized Python function to compute the Fibonacci sequence\n\n"
params:
include_stop_sequence: false
- "\nFind a random number between 1 and 20\n"
Expand All @@ -12,17 +12,17 @@ document:
import random
result = random.randint(1, 20)
show_result: true
- "\nNow computing fibonacci("
- "\nNow computing fib("
- get: N
- ")\n"
- def: RESULT
lan: python
code: |
{{ CODE }}
result = fibonacci({{ N }})
result = fib({{ N }})
show_result: false
- 'The result is: '
- get: RESULT
- "\n\nExplain what the above code does and what the result means\n\n"
- model: ibm/granite-20b-code-instruct
- "\n"
- "\n"
25 changes: 25 additions & 0 deletions examples/roles/chatbot.pdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
description: basic chatbot
document:
- role: system
document:
- |
You are Granite Chat, an AI language model developed by the IBM DMF Alignment Team.
You are a cautious assistant that carefully follows instructions.
You are helpful and harmless and you follow ethical guidelines and promote positive behavior.
You respond in a comprehensive manner unless instructed otherwise, providing explanations when needed while
maintaining a neutral tone. You are capable of coding, writing, and roleplaying. You are cautious and
refrain from generating real-time information, highly subjective or opinion-based topics.
You are harmless and refrain from generating content involving any form of bias, violence,
discrimination or inappropriate content. You always respond to greetings (for example, hi, hello, g'day,
morning, afternoon, evening, night, what's up, nice to meet you, sup, etc) with "Hello! I am Granite Chat,
created by the IBM DMF Alignment Team. How can I help you today?". Please do not say anything else
and do not start a conversation.
- repeat:
- role: user
read:
message: ""
def: query
- role: assistant
model: ibm/granite-13b-chat-v2
- "\n"
until: "{{ query == 'stop'}}"
14 changes: 14 additions & 0 deletions examples/roles/marketing.pdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
description: marketing email with roles
document:
- role: system
document:
- |
You are Granite Chat, an AI language model developed by IBM. You are a cautious assistant.
You carefully follow instructions. You are helpful and harmless and you follow ethical
guidelines and promote positive behavior.
- role: user
document:
" {{ instructions }}"
- role: assistant
document:
- model: ibm/granite-13b-chat-v2
3 changes: 3 additions & 0 deletions examples/talk/1-hello.pdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
description: My first PDL program
document:
- Hello, World!
24 changes: 24 additions & 0 deletions examples/talk/10-tools.pdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
document:
- |-
What is 18 + 12 x 3?
Act: {"name": "Calc", "arguments": {"expr": "18 + 12 * 3"}}
Obs: 54

A total of 252 qualifying matches were played, and 723 goals were scored. What was the average number of goals per match?
Act: {"name": "Calc", "arguments": {"expr": "723 / 252"}}
Obs: 2.869047619047619

Out of 1400 participants, 400 passed the test. What percentage is that?
Act:
- def: action
model: ibm/granite-8b-code-instruct
params:
stop_sequences: ["\n"]
parser: json
spec: {name: str, arguments: {expr: str}}
- if: '{{ action.name == "Calc" }}'
then:
- "Obs: "
- lan: python
code: result = {{ action.arguments.expr }}
- "\n"
50 changes: 50 additions & 0 deletions examples/talk/11-react.pdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
document:
- |
What is the elevation range for the area that the eastern sector of the Colorado orogeny extends into?
Tho: I need to search Colorado orogeny, find the area that the eastern sector of the Colorado ...
Act: {"name": "Search", "arguments": {"topic": "Colorado orogeny"}}
Obs: The Colorado orogeny was an episode of mountain building (an orogeny) ...
Tho: It does not mention the eastern sector. So I need to look up eastern sector.
Tho: High Plains rise in elevation from around 1,800 to 7,000 ft, so the answer is 1,800 to 7,000 ft.
Act: {"name": "Finish", "arguments": {"topic": "1,800 to 7,000 ft"}}

What profession does Nicholas Ray and Elia Kazan have in common?
Tho: I need to search Nicholas Ray and Elia Kazan, find their professions, then find the profession they have in common.
Act: {"name": "Search", "arguments": {"topic": "Nicholas Ray"}}
Obs: Nicholas Ray (born Raymond Nicholas Kienzle Jr., August 7, 1911 - June 16, 1979) was an American film director, screenwriter, and actor best known for the 1955 film Rebel Without a Cause.
Tho: Professions of Nicholas Ray are director, screenwriter, and actor. I need to search Elia Kazan next and find his professions.
Act: {"name": "Search", "arguments": {"topic": "Elia Kazan"}}
Obs: Elia Kazan was an American film and theatre director, producer, screenwriter and actor.
Tho: Professions of Elia Kazan are director, producer, screenwriter, and actor. So profession Nicholas Ray and Elia Kazan have in common is director, screenwriter, and actor.
Act: {"name": "Finish", "arguments": {"topic": "director, screenwriter, actor"}}

When was the discoverer of the Hudson River born?
- repeat:
document:
- def: thought
model: ibm/granite-34b-code-instruct
params:
STOP_SEQUENCES: ["Act:"]
INCLUDE_STOP_SEQUENCE: true
- def: action
model: ibm/granite-34b-code-instruct
params:
STOP_SEQUENCES: ["\n"]
parser: json
spec: {name: str, arguments: {topic: str}}
- def: observation
if: '{{ action.name == "Search" }}'
then:
document:
- "Obs: "
- lan: python
code: |
import warnings, wikipedia
warnings.simplefilter("ignore")
try:
result = wikipedia.summary("{{ action.arguments.topic }}")
except wikipedia.WikipediaException as e:
result = str(e)
until: '{{ action.name != "Search" }}'
as: document
- "\n"
38 changes: 38 additions & 0 deletions examples/talk/12-multiagent.pdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
document:
- include: ../prompt_library/tools.pdl
- include: ../prompt_library/ReAct.pdl
- include: ../granite/granite_defs.pdl
- def: filtered_tools
call: filter_tools_by_name
show_result: false
args:
tools: "{{ default_tools }}"
tool_names: ["Search"]
- def: QUESTION
read:
message: "Please enter a question: "
- def: PROPOSED
call: react_json
args:
context:
- role: system
content: "{{ granite_models.granite_7b_lab.system_prompt }}"
question: "{{ QUESTION }}"
model: ibm/granite-34b-code-instruct
tools: "{{ filtered_tools }}"
trajectories: []
- "\n\n----- Verifying answer... -----\n\n"
- def: VERIFIED
call: react_json
args:
context: [{"role": "system", "content": ""}]
question: |-
Is this the right answer to this question?
"{{ QUESTION }}"
Proposed answer: {{ PROPOSED.answer.topic }}

Please answer as True or False.
model: ibm/granite-34b-code-instruct
tools: "{{ filtered_tools }}"
trajectories: []
- "\n\nThe answer '{{ PROPOSED.answer.topic }}' has been verified as '{{VERIFIED.answer.topic}}'.\n"
8 changes: 8 additions & 0 deletions examples/talk/2-hello.pdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
description: My first PDL program
document:
- Hello,
- model: ibm/granite-34b-code-instruct
params:
stop_sequences: ["!"]
include_stop_sequence: true
- "\n"
13 changes: 13 additions & 0 deletions examples/talk/3-hello.pdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
description: My first PDL program
document:
- Hello,
- model: ibm/granite-34b-code-instruct
params:
stop_sequences: ["!"]
include_stop_sequence: true
- "\nTranslate this to French\n"
- model: ibm/granite-20b-multilingual
params:
stop_sequences: ["!"]
include_stop_sequence: true
- "\n"
17 changes: 17 additions & 0 deletions examples/talk/4-hello.pdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
description: My first PDL program
document:
- Hello,
- model: ibm/granite-34b-code-instruct
def: name
params:
stop_sequences: ["!"]
include_stop_sequence: false
- "\n"
- model: ibm/granite-20b-multilingual
input:
document:
- "Translate the word '{{ name | trim }}' to French\n"
params:
stop_sequences: ["!"]
include_stop_sequence: true
- "\n"
20 changes: 20 additions & 0 deletions examples/talk/5-hello.pdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
document:
- def: translate
function:
sentence: str
language: str
return:
- "\nTranslate the sentence '{{ sentence }}' to {{ language }}.\n"
- model: ibm/granite-20b-multilingual
params:
stop_sequences: ["\n"]
- call: translate
args:
sentence: I love Paris!
language: French
- "\n"
- call: translate
args:
sentence: I love Madrid!
language: Spanish
- "\n"
Loading

0 comments on commit 99c7d45

Please sign in to comment.