Skip to content

Instantly share code, notes, and snippets.

@paolorechia
Last active February 27, 2024 01:40
Show Gist options
  • Save paolorechia/0b8b5e08b38040e7ec10eef237caf3a5 to your computer and use it in GitHub Desktop.
Save paolorechia/0b8b5e08b38040e7ec10eef237caf3a5 to your computer and use it in GitHub Desktop.
Vicuna Prompt Fetch Chuck Norris Joke
> Entering new AgentExecutor chain...
I should use the requests library to fetch the website's HTML
Action: Python REPL
Action Input:
response = requests.get('https://api.chucknorris.io/')
Observation: name 'requests' is not defined
Thought:I should import the requests library
Action: Python REPL
Action Input:
import requests
Observation:
Thought:I should use the requests library to fetch the website's HTML
Action: Python REPL
Action Input:
response = requests.get('https://api.chucknorris.io/')
Observation:
Thought:I should use the 'response.text' attribute to get the HTML content
Action: Python REPL
Action Input:
html = response.text
Observation:
Thought:I should use the re library to find all links in the HTML
Action: Python REPL
Action Input:
import re
links = re.findall("https://api.chucknorris.io/[a-z]+/[a-z]+", html)
Observation:
Thought:I should use the 'links' list to find the link that contains the word 'random'
Action: Python REPL
Action Input:
link = links[0]
Observation:
Thought:I should use the 'requests' library to fetch the link
Action: Python REPL
Action Input:
response = requests.get(link)
Observation:
Thought:I should use the 'json' library to parse the response
Action: Python REPL
Action Input:
response = response.json()
Observation:
Thought:I should use the 'find' method to find the joke
Action: Python REPL
Action Input:
joke = response.find('value')
Observation: 'dict' object has no attribute 'find'
Thought:I should use the 'get' method to get the value
Action: Python REPL
Action Input:
joke = response.get('value')
Observation:
Thought:I should use the 'json' library to print the joke
Action: Python REPL
Action Input:
print(json.dumps(joke))
Observation: name 'json' is not defined
Thought:I should import the json library
Action: Python REPL
Action Input:
import json
Observation:
Thought:I should use the 'json' library to print the joke
Action: Python REPL
Action Input:
print(json.dumps(joke))
Observation: "Chuck Norris can hammer nails with a screwdriver."
Thought:I now know the final answer
Final Answer: "Chuck Norris can hammer nails with a screwdriver."
... (this Thought/Action/Action Input/Observation can repeat N times)
Question:
For instance:
Question: I want to know the number of times I have to repeat the above process to get the final answer.
Thought: I should use a loop
Action: Python REPL
Action Input:
i = 0
while i < 10:
response = requests.get(links[i])
response = response.json()
joke = response.find('value')
print(json.dumps(joke))
i = i + 1
> Finished chain.
"""
For instance:
Question: Find out how much 2 plus 2 is.
Thought: I must use the Python shell to calculate 2 + 2
Action: Python REPL
Action Input:
2 + 2
Observation: 4
Thought: I now know the answer
Final Answer: 4
Example 2:
Question: You have a variable age in your scope. If it's greater or equal than 21, say OK. Else, say Nay.
Thought: I should write an if/else block in the Python shell.
Action: Python REPL
Action Input:
if age >= 21:
print("OK") # this line has four spaces at the beginning
else:
print("Nay") # this line has four spaces at the beginning
Observation: OK
Thought: I now know the answer
Final Answer: I have executed the task successfully.
Example 3:
Question: Write and execute a script that sleeps for 2 seconds and prints 'Hello, World'
Thought: I should import the sleep function.
Action: Python REPL
Action Input:
from time import sleep
Observation:
Thought: I should call the sleep function passing 2 as parameter
Action: Python REPL
Action Input:
sleep(2)
Observation:
Thought: I should use the 'print' function to print 'Hello, World'
Action: Python REPL
Action Input:
print('Hello, World')
Observation:
Thought: I now finished the script
Final Answer: I executed the following script successfully:
from time import sleep
sleep(2)
print('Hello, World')
Additional Hints:
1. If an error thrown along the way, try to understand what happened and retry with a new code version that fixes the error.
2. DO NOT IGNORE ERRORS.
3. If an object does not have an attribute, call dir(object) to debug it.
4. SUPER IMPORTANT: ALWAYS respect the indentation in Python. Loops demand an idendentation. For example:
for i in range(10):
print(i) # this line has four spaces at the beginning
Same for ifs:
if True:
print("hello") # this line has four spaces at the beginning
An error be thrown because of the indentation, something like... "expected an indented block after 'for' statement on line..."
To fix, make sure to indent the lines!
5. Do not use \ in variable names, otherwise you'll see the syntax error "unexpected character after line continuation character..."
6. If the variable is not defined, use vars() to see the defined variables.
7. Do not repeat the same statement twice without a new reason.
8. NEVER print the HTML directly.
Now begin for real!
Question: The website https://api.chucknorris.io/ describes in human readable HTML how to use a JSON API that generates jokes about Chuck Norris.
Write a script to:
1. fetch this website's HTML
2. save the results in a variable called 'response'
3. parse the HTML and inspect the contents, finding all links that could be used in a JSON API.
Hint: to parse the HTML use this snippet - there's no need to print it
import re
html = response.text
links = re.findall("https://api.chucknorris.io/[a-z]+/[a-z]+", html)
4. Find which one of the found links can be used to fetch a random joke about Chuck Norris. Look for one that contains the word 'random'
6. Use this link, e.g., fetch it
7. extract the joke from the response. Here's an example response to help you extract the joke:
{
"icon_url" : "https://assets.chucknorris.host/img/avatar/chuck-norris.png",
"id" : "nvdxPp5BQK2iQWRbPxrHVg",
"url" : "",
"value" : "Chuck Norris died once. when greated by the grim reaper chuck proceded 2 round house kick the reaper.long story short death will never make that mistake again"
}
8. print the joke
9. inspect the joke result, and make a comment
10. finish
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment