总结就是:通过实践、提问、利用 AI 辅助工具、接受错误、深入理解和自主学习,可以更有效地学习编程,并享受这一过程。
4 chatbots 彩蛋
课程中很多章节中有一些内置的函数,其实可以新建 cell,然后执行 linux 命令看到.py 和 system_prompt 具体内容
学习大佬是如何写 Prompt 的
# helper_functions.py
import os
from openai import OpenAI from dotenv import load_dotenv
import random
#Get the OpenAI API key from the .env file load_dotenv('.env', override=True) openai_api_key = os.getenv('OPENAI_API_KEY')
# Set up the OpenAI client client = OpenAI(api_key=openai_api_key)
defprint_llm_response(prompt): """This function takes as input a prompt, which must be a string enclosed in quotation marks, and passes it to OpenAI's GPT3.5 model. The function then prints the response of the model. """ llm_response = get_llm_response(prompt) print(llm_response)
defget_llm_response(prompt): """This function takes as input a prompt, which must be a string enclosed in quotation marks, and passes it to OpenAI's GPT3.5 model. The function then saves the response of the model as a string. """ try: ifnot isinstance(prompt, str): raise ValueError("Input must be a string enclosed in quotes.") completion = client.chat.completions.create( model="gpt-3.5-turbo-0125", messages=[ { "role": "system", "content": "You are a helpful but terse AI assistant who gets straight to the point.", }, {"role": "user", "content": prompt}, ], temperature=0.0, ) response = completion.choices[0].message.content return response except TypeError as e: print("Error:", str(e))
defget_chat_completion(prompt, history): history_string = "\n\n".join(["\n".join(turn) for turn in history]) prompt_with_history = f"{history_string}\n\n{prompt}" completion = client.chat.completions.create( model="gpt-3.5-turbo-0125", messages=[ { "role": "system", "content": "You are a helpful but terse AI assistant who gets straight to the point.", }, {"role": "user", "content": prompt_with_history}, ], temperature=0.0, ) response = completion.choices[0].message.content return response
# def open_chatbot(): # """This function opens a Gradio chatbot window that is connected to OpenAI's GPT3.5 model.""" # gr.close_all() # gr.ChatInterface(fn=get_chat_completion).launch(quiet=True)
defget_dog_age(human_age): """This function takes one parameter: a person's age as an integer and returns their age if they were a dog, which is their age divided by 7. """ return human_age / 7
defget_goldfish_age(human_age): """This function takes one parameter: a person's age as an integer and returns their age if they were a dog, which is their age divided by 5. """ return human_age / 5
defget_cat_age(human_age): if human_age <= 14: # For the first 14 human years, we consider the age as if it's within the first two cat years. cat_age = human_age / 7 else: # For human ages beyond 14 years: cat_age = 2 + (human_age - 14) / 4 return cat_age
defget_random_ingredient(): """ Returns a random ingredient from a list of 20 smoothie ingredients.
The ingredients are a bit wacky but not gross, making for an interesting smoothie combination.
You are the friendly AI assistant for a beginner python programming class. You are available to help learners with questions they might have about computer programming, python, artificial intelligence, the internet, and other related topics.
You should assume zero to very little prior experience of coding when you reply to questions. You should only use python and not mention other programming languages (unless the question is about how computers work, where you may mention assembly or machine code if it is relevant to the answer).
Only write code if you are asked directly by the learner. If you do write any code, it should be as simple and easy to read as possible - name variables things that are easy to understand, and avoid pythonic conventions like list comprehensions to help the learner stick to foundations like for loops and if statements.
Keep your answers to questions short, offering as little explanation as is necessary to answer the question. Let the learner ask follow up questions to dig deeper.
If the learner asks unrelated questions, respond with a brief reminder: "Please, focus on your programming for AI journey"