diff --git a/fern/pages/llm-university/intro-text-generation/building-a-chatbot.mdx b/fern/pages/llm-university/intro-text-generation/building-a-chatbot.mdx index 454e24db2..b6d215515 100644 --- a/fern/pages/llm-university/intro-text-generation/building-a-chatbot.mdx +++ b/fern/pages/llm-university/intro-text-generation/building-a-chatbot.mdx @@ -20,7 +20,8 @@ To set up, we first import the Cohere module and create a client. ```python PYTHON import cohere -co = cohere.Client("COHERE_API_KEY") # Your Cohere API key + +co = cohere.Client("COHERE_API_KEY") # Your Cohere API key ``` At its most basic, we only need to pass to the Chat endpoint the user message using the `message` parameter – the only required parameter for the endpoint. @@ -53,9 +54,11 @@ In the quickstart example, we didn’t have to define a preamble because a defau Here’s an example. We added a preamble telling the chatbot to assume the persona of an expert public speaking coach. As a result, we get a response that adopts that persona. ```python PYTHON -response = co.chat(message="Hello", - model="command-r-plus", - preamble="You are an expert public speaking coach. Don't use any greetings.") +response = co.chat( + message="Hello", + model="command-r-plus", + preamble="You are an expert public speaking coach. Don't use any greetings.", +) print(response.text) ``` @@ -78,13 +81,15 @@ In streaming mode, the endpoint will generate a series of objects. To get the ac If you have not already, make your own copy of the Google Colaboratory notebook and run the code in this section to see the same example with streamed responses activated. ```python PYTHON -stream = co.chat_stream(message="Hello. I'd like to learn about techniques for effective audience engagement", - model="command-r-plus", - preamble="You are an expert public speaking coach") +stream = co.chat_stream( + message="Hello. I'd like to learn about techniques for effective audience engagement", + model="command-r-plus", + preamble="You are an expert public speaking coach", +) for event in stream: if event.event_type == "text-generation": - print(event.text, end='') + print(event.text, end="") ``` ``` @@ -149,21 +154,23 @@ while True: message = input("User: ") # Typing "quit" ends the conversation - if message.lower() == 'quit': + if message.lower() == "quit": print("Ending chat.") break # Chatbot response - stream = co.chat_stream(message=message, - model="command-r-plus", - preamble=preamble, - conversation_id=conversation_id) + stream = co.chat_stream( + message=message, + model="command-r-plus", + preamble=preamble, + conversation_id=conversation_id, + ) - print("Chatbot: ", end='') + print("Chatbot: ", end="") for event in stream: if event.event_type == "text-generation": - print(event.text, end='') + print(event.text, end="") if event.event_type == "stream-end": chat_history = event.response.chat_history @@ -237,12 +244,12 @@ The chat history is a list of multiple turns of messages from the user and the c from cohere import ChatMessage chat_history = [ - ChatMessage(role="USER", message="What is 2 + 2"), - ChatMessage(role="CHATBOT", message="The answer is 4"), - ChatMessage(role="USER", message="Add 5 to that number"), - ChatMessage(role="CHATBOT", message="Sure. The answer is 9"), - ... - ] + ChatMessage(role="USER", message="What is 2 + 2"), + ChatMessage(role="CHATBOT", message="The answer is 4"), + ChatMessage(role="USER", message="Add 5 to that number"), + ChatMessage(role="CHATBOT", message="Sure. The answer is 9"), + ..., +] ``` The following modifies the previous implementation by using `chat_history` instead of `conversation_id` for managing the conversation history. @@ -262,29 +269,33 @@ while True: message = input("User: ") # Typing "quit" ends the conversation - if message.lower() == 'quit': + if message.lower() == "quit": print("Ending chat.") break # Chatbot response - stream = co.chat_stream(message=message, - model="command-r-plus", - preamble=preamble, - chat_history=chat_history) + stream = co.chat_stream( + message=message, + model="command-r-plus", + preamble=preamble, + chat_history=chat_history, + ) chatbot_response = "" - print("Chatbot: ", end='') + print("Chatbot: ", end="") for event in stream: if event.event_type == "text-generation": - print(event.text, end='') + print(event.text, end="") chatbot_response += event.text print("\n") # Add to chat history chat_history.extend( - [ChatMessage(role="USER", message=message), - ChatMessage(role="CHATBOT", message=chatbot_response)] + [ + ChatMessage(role="USER", message=message), + ChatMessage(role="CHATBOT", message=chatbot_response), + ] ) ``` diff --git a/fern/pages/llm-university/intro-text-generation/creating-custom-models.mdx b/fern/pages/llm-university/intro-text-generation/creating-custom-models.mdx index a6076f50b..7a1f12d26 100644 --- a/fern/pages/llm-university/intro-text-generation/creating-custom-models.mdx +++ b/fern/pages/llm-university/intro-text-generation/creating-custom-models.mdx @@ -134,9 +134,10 @@ Using a custom model is as simple as substituting the baseline model with the mo ```python PYTHON response = co.generate( - model='26db2994-cf88-4243-898d-31258411c120-ft', # REPLACE WITH YOUR MODEL ID - prompt="""Turn the following message to a virtual assistant into the correct action: - Send a message to Alison to ask if she can pick me up tonight to go to the concert together""") + model="26db2994-cf88-4243-898d-31258411c120-ft", # REPLACE WITH YOUR MODEL ID + prompt="""Turn the following message to a virtual assistant into the correct action: + Send a message to Alison to ask if she can pick me up tonight to go to the concert together""", +) ``` But of course, we need to know if this model is performing better than the baseline in the first place. For this, there are a couple of ways we can evaluate our model. @@ -178,17 +179,19 @@ We run the following code for each of the baseline (`command`) and the finetuned ```python PYTHON # Create a function to call the endpoint -def generate_text(prompt,temperature,num_gens): - response = co.generate( - model='command', # Repeat with the custom model - prompt=prompt, - temperature=temperature, - num_generations = num_gens, - stop_sequences=["\n\n"]) - return response +def generate_text(prompt, temperature, num_gens): + response = co.generate( + model="command", # Repeat with the custom model + prompt=prompt, + temperature=temperature, + num_generations=num_gens, + stop_sequences=["\n\n"], + ) + return response + # Define the prompt -prompt="""Turn the following message to a virtual assistant into the correct action: +prompt = """Turn the following message to a virtual assistant into the correct action: Send a message to Alison to ask if she can pick me up tonight to go to the concert together""" # Define the range of temperature values and num_generations @@ -198,14 +201,14 @@ num_gens = 3 # Iterate generation over the range of temperature values print(f"Temperature range: {temperatures}") for temperature in temperatures: - response = generate_text(prompt,temperature,num_gens) - print("-"*10) - print(f'Temperature: {temperature}') - print("-"*10) - for i in range(3): - text = response.generations[i].text - print(f'Generation #{i+1}') - print(f'Text: {text}\n') + response = generate_text(prompt, temperature, num_gens) + print("-" * 10) + print(f"Temperature: {temperature}") + print("-" * 10) + for i in range(3): + text = response.generations[i].text + print(f"Generation #{i+1}") + print(f"Text: {text}\n") ``` Here are the responses. diff --git a/fern/pages/llm-university/intro-text-generation/fine-tuning-for-chat.mdx b/fern/pages/llm-university/intro-text-generation/fine-tuning-for-chat.mdx index a871e69b3..f67b23a11 100644 --- a/fern/pages/llm-university/intro-text-generation/fine-tuning-for-chat.mdx +++ b/fern/pages/llm-university/intro-text-generation/fine-tuning-for-chat.mdx @@ -108,21 +108,20 @@ user_message = "Make the text coherent: Pimelodella kronei is a species of three preamble = "You are a writing assistant that helps the user write coherent text." # Get default model response -response_pretrained=co.chat( - message=user_message, - preamble=preamble, - ) +response_pretrained = co.chat( + message=user_message, + preamble=preamble, +) # Get fine-tuned model response response_finetuned = co.chat( - message=user_message, - model='acb944bb-fb49-4c29-a15b-e6a245a7bdf9-ft', - preamble=preamble, - ) + message=user_message, + model="acb944bb-fb49-4c29-a15b-e6a245a7bdf9-ft", + preamble=preamble, +) -print(f"Default response: {response_pretrained.text}","\n-----") +print(f"Default response: {response_pretrained.text}", "\n-----") print(f"Fine-tuned response: {response_finetuned.text}") - ``` For this example, the output appears as follows: @@ -159,14 +158,14 @@ while True: # Typing "quit" ends the conversation if message.lower() == 'quit': - print("Ending chat.") - break + print("Ending chat.") + break # Chatbot response stream = co.chat_stream(message=message, - model='acb944bb-fb49-4c29-a15b-e6a245a7bdf9-ft', - preamble=preamble, - conversation_id=conversation_id) + model='acb944bb-fb49-4c29-a15b-e6a245a7bdf9-ft', + preamble=preamble, + conversation_id=conversation_id) print("Chatbot: ", end='') diff --git a/fern/pages/llm-university/intro-text-generation/parameters-for-controlling-outputs.mdx b/fern/pages/llm-university/intro-text-generation/parameters-for-controlling-outputs.mdx index 47451d5e1..03d1c66ad 100644 --- a/fern/pages/llm-university/intro-text-generation/parameters-for-controlling-outputs.mdx +++ b/fern/pages/llm-university/intro-text-generation/parameters-for-controlling-outputs.mdx @@ -18,7 +18,8 @@ To set up, we first import the Cohere module and create a client. ```python PYTHON import cohere -co = cohere.Client("COHERE_API_KEY") # Your Cohere API key + +co = cohere.Client("COHERE_API_KEY") # Your Cohere API key ``` ## Model Type @@ -35,8 +36,7 @@ With the [Chat endpoint](/reference/chat) , you can choose from several variatio Use the `model` parameter to select a variation that suits your requirements. In the code cell, we select `command-r`. ```python PYTHON -response = co.chat(message="Hello", - model="command-r-plus") +response = co.chat(message="Hello", model="command-r-plus") print(response.text) ``` @@ -80,9 +80,9 @@ message = """Suggest a more exciting title for a blog post titled: Intro to Retr Respond in a single line.""" for _ in range(5): - response = co.chat(message=message, - temperature=0, - model="command-r-plus") + response = co.chat( + message=message, temperature=0, model="command-r-plus" + ) print(response.text) ``` @@ -103,9 +103,9 @@ message = """Suggest a more exciting title for a blog post titled: Intro to Retr Respond in a single line.""" for _ in range(5): - response = co.chat(message=message, - temperature=1, - model="command-r-plus") + response = co.chat( + message=message, temperature=1, model="command-r-plus" + ) print(response.text) ``` diff --git a/fern/pages/llm-university/intro-text-generation/prompt-engineering-basics.mdx b/fern/pages/llm-university/intro-text-generation/prompt-engineering-basics.mdx index 786367428..b189ce52a 100644 --- a/fern/pages/llm-university/intro-text-generation/prompt-engineering-basics.mdx +++ b/fern/pages/llm-university/intro-text-generation/prompt-engineering-basics.mdx @@ -20,7 +20,8 @@ To set up, we first import the Cohere module and create a client. ```python PYTHON import cohere -co = cohere.Client("COHERE_API_KEY") # Your Cohere API key + +co = cohere.Client("COHERE_API_KEY") # Your Cohere API key ``` Let's also define a function `generate_text()` to take a user message, call the Chat endpoint, and stream the response. @@ -30,7 +31,7 @@ def generate_text(message): stream = co.chat_stream(message=message, model="command-r-plus") for event in stream: if event.event_type == "text-generation": - print(event.text, end='') + print(event.text, end="") ``` ## Writing a Basic Prompt @@ -40,7 +41,9 @@ The best way to design prompts for a model like [Command](https://cohere.com/mod For instance, let’s say that we are creating the product description copy for a wireless earbuds product. We can write the prompt as follows. ```python PYTHON -generate_text("Generate a concise product description for the product: wireless earbuds.") +generate_text( + "Generate a concise product description for the product: wireless earbuds." +) ``` ``` @@ -60,10 +63,12 @@ But perhaps we want to be more specific regarding what we want the output to loo Let’s say we want the model to write the product description in a particular format with specific information. In this case, we can append this specific instruction in the prompt. ```python PYTHON -generate_text(""" +generate_text( + """ Generate a concise product description for the product: wireless earbuds. Use the following format: Hook, Solution, Features and Benefits, Call to Action. - """) + """ +) ``` ``` @@ -92,7 +97,8 @@ The model returns an output following the format that we wanted. The prompt can also be constructed as a combination of an instruction and some context. Let’s see this in action with another example: emails. We can create a prompt to summarize an email, which is included in the prompt for context. ```python PYTHON -generate_text(""" +generate_text( + """ Summarize this email in one sentence. Dear [Team Members], I am writing to thank you for your hard work and dedication in organizing our recent community meetup. The event was a great success and it would not have been possible without your efforts. @@ -102,7 +108,8 @@ generate_text(""" Thank you again for your hard work and dedication. I am looking forward to working with you on future events. Sincerely, [Your Name] - """) + """ +) ``` ``` @@ -118,10 +125,12 @@ This instruction–context prompt format is extremely useful as it means that we Let's move to another example — an extraction task, which a generative model can do very well. Given context, which in this case is a description of a movie, we want the model to extract the movie title. ```python PYTHON -generate_text(""" +generate_text( + """ Extract the movie title from the text below. Deadpool 2 | Official HD Deadpool's "Wet on Wet" Teaser | 2018 - """) + """ +) ``` ``` @@ -137,7 +146,8 @@ The model is also effective at tasks that involve taking a piece of text and rew Here's an example. We have a one-line instruction followed by the context, which in this case is a blog excerpt. The instruction is to generate a list of frequently asked questions (FAQ) based on the passage, which involves a mixture of several tasks, such as extraction and rewriting. ```python PYTHON -generate_text(""" +generate_text( + """ Given the following text, write down a list of potential frequently asked questions (FAQ), together with the answers. The Cohere Platform provides an API for developers and organizations to access cutting-edge LLMs without needing machine learning know-how. The platform handles all the complexities of curating massive amounts of text data, model development, distributed training, model serving, and more. @@ -150,7 +160,8 @@ generate_text(""" With text embedding, we enter a piece of text and get back a list of numbers that represents its semantic meaning (we’ll see what “semantic” means in a section below). This is useful for use cases that involve “measuring” what a passage of text represents, for example, in analyzing its sentiment. - """) + """ +) ``` ``` diff --git a/fern/pages/llm-university/intro-text-generation/the-generate-endpoint.mdx b/fern/pages/llm-university/intro-text-generation/the-generate-endpoint.mdx index 076a11ca2..7bbb22f84 100644 --- a/fern/pages/llm-university/intro-text-generation/the-generate-endpoint.mdx +++ b/fern/pages/llm-university/intro-text-generation/the-generate-endpoint.mdx @@ -18,9 +18,10 @@ We enter a prompt: ```python PYTHON response = co.generate( - model='command', - prompt='Generate a social ad copy for the product: Wireless Earbuds.', - max_tokens=100) + model="command", + prompt="Generate a social ad copy for the product: Wireless Earbuds.", + max_tokens=100, +) print(response.generations[0].text) ``` @@ -77,12 +78,14 @@ Import the Cohere package, define the Cohere client with the API key, and add th ```python PYTHON import cohere -co = cohere.Client('your_api_key') + +co = cohere.Client("your_api_key") response = co.generate( - model='command', - prompt='Generate a concise product description for the product: wireless earbuds.', - max_tokens=100) + model="command", + prompt="Generate a concise product description for the product: wireless earbuds.", + max_tokens=100, +) print(response.generations[0].text) ``` @@ -168,10 +171,11 @@ Note that you need to enable the `return_likelihoods` parameter to return either ```python PYTHON response = co.generate( - model='command', - prompt='Generate a concise product description for the product: wireless earbuds.', - max_tokens=100, - return_likelihoods='GENERATION') + model="command", + prompt="Generate a concise product description for the product: wireless earbuds.", + max_tokens=100, + return_likelihoods="GENERATION", +) print(response) ``` @@ -234,17 +238,19 @@ And here’s what the code looks like: ```python PYTHON # Function to call the Generate endpoint -def generate_text(prompt,temperature,num_gens): - response = co.generate( - model='command', - prompt=prompt, - temperature=temperature, - num_generations = num_gens, - return_likelihoods='GENERATION') - return response +def generate_text(prompt, temperature, num_gens): + response = co.generate( + model="command", + prompt=prompt, + temperature=temperature, + num_generations=num_gens, + return_likelihoods="GENERATION", + ) + return response + # Define the prompt -prompt='Generate a concise product description for the product: wireless earbuds.' +prompt = "Generate a concise product description for the product: wireless earbuds." # Define the range of temperature values and num_generations temperatures = [x / 10.0 for x in range(0, 60, 10)] @@ -253,13 +259,13 @@ num_gens = 3 # Iterate over the range of temperature values print(f"Temperature range: {temperatures}") for temperature in temperatures: - response = generate_text(prompt,temperature,num_gens) - for i in range(3): - text = response.generations[i].text - likelihood = response.generations[i].likelihood - print(f'Generation #{i+1}') - print(f'Text: {text}\n') - print(f'Likelihood: {likelihood}\n') + response = generate_text(prompt, temperature, num_gens) + for i in range(3): + text = response.generations[i].text + likelihood = response.generations[i].likelihood + print(f"Generation #{i+1}") + print(f"Text: {text}\n") + print(f"Likelihood: {likelihood}\n") ``` You can run the code in the notebook and get the full generation. Here, we are showing a few example outputs, as the full generation is quite long (view the notebook to see the full generation).