Skip to content

Commit

Permalink
reformat prompt-engineering section (#341)
Browse files Browse the repository at this point in the history
Co-authored-by: Max Shkutnyk <max@lightsonsoftware.com>
  • Loading branch information
invader89 and Max Shkutnyk authored Jan 9, 2025
1 parent ba8fa42 commit b6700cc
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,16 @@ Using the Chat API, we could do the following:

```python PYTHON
import cohere

co = cohere.ClientV2(api_key="<YOUR API KEY>")

example = '''On the issue of Albert's wellbeing after the accident, Angela testified that he
gave a thumbs up when asked how he was feeling.'''
message = f'''{example} Is there hearsay?'''
example = """On the issue of Albert's wellbeing after the accident, Angela testified that he
gave a thumbs up when asked how he was feeling."""
message = f"""{example} Is there hearsay?"""

response = co.chat(
messages=[{'role': 'user', 'content': message}],
model='command-r',
messages=[{"role": "user", "content": message}],
model="command-r",
temperature=0.3,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Then use the Chat API to send a message to the model:

```python PYTHON
import cohere

co = cohere.ClientV2(api_key="<YOUR API KEY>")

message = """
Expand All @@ -42,9 +43,9 @@ Summarize the text below.

# get model response
response = co.chat(
messages=[{'role': 'user', 'content': message}],
model="command-r-plus-08-2024",
temperature=0.3
messages=[{"role": "user", "content": message}],
model="command-r-plus-08-2024",
temperature=0.3,
)
```

Expand Down Expand Up @@ -77,21 +78,21 @@ For the example above, we can chunk a news article into different sections and a
```python PYTHON
# Sections from the original news article
document_chunked = [
{
"data": {
"text": "Equipment rental in North America is predicted to “normalize” going into 2024, according to Josh Nickell, vice president of equipment rental for the American Rental Association (ARA)."
}
},
{
"data": {
"text": "“Rental is going back to ‘normal,’ but normal means that strategy matters again - geography matters, fleet mix matters, customer type matters,” Nickell said. “In late 2020 to 2022, you just showed up with equipment and you made money."
}
},
{
"data": {
"text": "“Everybody was breaking records, from the national rental chains to the smallest rental companies; everybody was having record years, and everybody was raising prices. The conversation was, ‘How much are you up?’ And now, the conversation is changing to ‘What’s my market like?’”"
}
}
{
"data": {
"text": "Equipment rental in North America is predicted to “normalize” going into 2024, according to Josh Nickell, vice president of equipment rental for the American Rental Association (ARA)."
}
},
{
"data": {
"text": "“Rental is going back to ‘normal,’ but normal means that strategy matters again - geography matters, fleet mix matters, customer type matters,” Nickell said. “In late 2020 to 2022, you just showed up with equipment and you made money."
}
},
{
"data": {
"text": "“Everybody was breaking records, from the national rental chains to the smallest rental companies; everybody was having record years, and everybody was raising prices. The conversation was, ‘How much are you up?’ And now, the conversation is changing to ‘What’s my market like?’”"
}
},
]

# Add a system message for additional context
Expand All @@ -101,10 +102,14 @@ You will receive a series of text fragments from a document that are presented i
# Call the model
message = f"Summarize this text in one sentence."

response = co.chat(model="command-r-plus-08-2024",
documents=document_chunked,
messages=[{"role": "system", "content": system_message},
{'role': 'user', "content": message}])
response = co.chat(
model="command-r-plus-08-2024",
documents=document_chunked,
messages=[
{"role": "system", "content": system_message},
{"role": "user", "content": message},
],
)

response_text = response.message.content[0].text

Expand Down Expand Up @@ -136,21 +141,30 @@ These can easily be rendered into the text to show the source of each piece of i
```python PYTHON
# Function to insert inline citations into the text
def insert_inline_citations(text, citations):
sorted_citations = sorted(citations, key=lambda c: c.start, reverse=True)

sorted_citations = sorted(
citations, key=lambda c: c.start, reverse=True
)

for citation in sorted_citations:
source_ids = [source.id.split(':')[-1] for source in citation.sources]
source_ids = [
source.id.split(":")[-1] for source in citation.sources
]
citation_text = f"[{','.join(source_ids)}]"
text = text[:citation.end] + citation_text + text[citation.end:]

text = (
text[: citation.end]
+ citation_text
+ text[citation.end :]
)

return text


# Function to list source documents
def list_sources(citations):
unique_sources = {}
for citation in citations:
for source in citation.sources:
source_id = source.id.split(':')[-1]
source_id = source.id.split(":")[-1]
if source_id not in unique_sources:
unique_sources[source_id] = source.document

Expand All @@ -160,11 +174,14 @@ def list_sources(citations):
for key, value in document.items():
footnote += f"{key}: {value}, "
footnotes.append(footnote.rstrip(", "))

return "\n".join(footnotes)


# Use the functions
cited_text = insert_inline_citations(response.message.content[0].text, response.message.citations)
cited_text = insert_inline_citations(
response.message.content[0].text, response.message.citations
)

# Print the result with inline citations
print(cited_text)
Expand Down
52 changes: 38 additions & 14 deletions fern/pages/v2/text-generation/prompt-engineering/preambles.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,21 @@ To set a custom system message, use the `system` role in the `messages` paramete

```python PYTHON
import cohere

co = cohere.ClientV2(api_key="<YOUR API KEY>")

response = co.chat(
model="command-r-plus-08-2024",
messages=[{'role': 'system', 'content': 'You are an overly enthusiastic model that responds to everything with a lot of punctuation'},
{'role': 'user', 'content': 'Come up with a great name for a cat'}]
messages=[
{
"role": "system",
"content": "You are an overly enthusiastic model that responds to everything with a lot of punctuation",
},
{
"role": "user",
"content": "Come up with a great name for a cat",
},
],
)

print(response.message.content[0].text)
Expand All @@ -64,56 +73,71 @@ The Command R model responds particularly well to system messages that follow a
Copy this template for best results in your custom system message.

```python PYTHON
system_message_template = '''
system_message_template = """
## Task and Context
----> TELL THE MODEL WHO IT IS AND WHAT IT DOES <----
## Style Guide
----> ADD INSTRUCTIONS FOR STYLISTIC CHOICES THE MODEL SHOULD MAKE <----
'''
"""
co.chat(
model="command-r-plus-08-2024",
messages=[{'role': 'system', 'content': system_message_template},
{'role': 'user', 'content': 'Where can I find the best burger in San Francisco?'}]
messages=[
{"role": "system", "content": system_message_template},
{
"role": "user",
"content": "Where can I find the best burger in San Francisco?",
},
],
)
```

### Example System Message 1

```python PYTHON
tour_guide_system_message = '''
tour_guide_system_message = """
## Task and Context
You are a tour guide in Toronto. You give walking tours peppered with fun facts about the history of the city. If someone asks you a question unrelated to Toronto, subtly yet firmly change the topic back to fun facts about Toronto.
## Style Guide
Use British/Canadian spelling of words, and try to speak in sonnets as much as possible. Be professional.
'''
"""

co.chat(
model="command-r-plus-08-2024",
messages=[{'role': 'system', 'content': tour_guide_system_message},
{'role': 'user', 'content': 'Where can I find the best burger in San Francisco?'}]
messages=[
{"role": "system", "content": tour_guide_system_message},
{
"role": "user",
"content": "Where can I find the best burger in San Francisco?",
},
],
)
```

### Example System Message 2

```python PYTHON
pirate_system_message='''
pirate_system_message = """
## Task and Context
You are a chatbot who talks with users about various nautical themes
## Style Guide
Always answer with ooh arrr. Talk like Pirate. Be as chatty and verbose as possible
'''
"""

co.chat(
model="command-r-plus-08-2024",
messages=[{'role': 'system', 'content': pirate_system_message},
{'role': 'user', 'content': 'What is the most dangerous thing about sailing?'}]
messages=[
{"role": "system", "content": pirate_system_message},
{
"role": "user",
"content": "What is the most dangerous thing about sailing?",
},
],
)
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,19 @@ co = cohere.ClientV2(api_key="<YOUR API KEY>")

response = co.chat(
model="command-r-plus-08-2024",
messages=[{"role": "user", "content": """
messages=[
{
"role": "user",
"content": """
You are a Python expert. For the given Python function, add mypy typing and a docstring. Return the Python function only.
```py
def add(a,b):
return a + b
```
"""}]
""",
}
],
)

print(response.message.content[0].text)

````
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@ import cohere

co = cohere.ClientV2(api_key="<YOUR API KEY>")

response = co.chat(model="command-r-plus-08-2024",
messages=[{"role": "user", "content": """
response = co.chat(
model="command-r-plus-08-2024",
messages=[
{
"role": "user",
"content": """
# Customer
I want to book an appointment for a haircut next Friday at 3pm.
Expand All @@ -81,7 +85,9 @@ response = co.chat(model="command-r-plus-08-2024",
next_available_time: "%Y-%m-%d %H"
}
```
"""}]
""",
}
],
)

print(response.message.content[0].text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ co = cohere.ClientV2(api_key="<YOUR API KEY>")

response = co.chat(
model="command-r-plus-08-2024",
messages=[{"role": "user", "content": """
messages=[
{
"role": "user",
"content": """
You are an expert in data formatting. For the following csv data, output it as a markdown table.
Output the table only.
Expand All @@ -55,8 +58,10 @@ response = co.chat(
Bob Johnson,42,Software Developer
Emily Davis,37,Product Manager
```
"""}]
)
""",
}
],
)

print(response.message.content[0].text)
````
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ co = cohere.ClientV2(api_key="<YOUR API KEY>")

response = co.chat(
model="command-r-plus-08-2024",
messages=[{"role": "user", "content": """
messages=[
{
"role": "user",
"content": """
You are an expert in data formatting. Convert the following JSON object into a CSV format.
```
Expand All @@ -80,7 +83,9 @@ response = co.chat(
},
]
```
"""}]
""",
}
],
)

print(response.message.content[0].text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ co = cohere.ClientV2(api_key="<YOUR API KEY>")

response = co.chat(
model="command-r-plus-08-2024",
messages=[{"role": "user", "content": """
messages=[
{
"role": "user",
"content": """
You are an AI grader that given an output and a criterion, grades the completion based on
the prompt and criterion. Below is a prompt, a completion, and a criterion with which to grade
the completion. You need to respond according to the criterion instructions.
Expand All @@ -57,7 +60,9 @@ response = co.chat(
## Criterion
Rate the ouput text with a score between 0 and 1. 1 being the text was written in a formal
and business appropriate tone and 0 being an informal tone. Respond only with the score.
"""}]
""",
}
],
)

print(response.message.content[0].text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,15 @@ co = cohere.ClientV2(api_key="<YOUR API KEY>")

response = co.chat(
model="command-r-plus-08-2024",
messages=[{"role": "user", "content": """
messages=[
{
"role": "user",
"content": """
... <content skipped> ...
Summarize the key events from this meeting in 2 clear and concise bullet points. Each bullet point should be very concise, with no more than 20 words. Use the active voice only. Do not use the passive voice. Order the bullets such that more general bullets come first. Do not include any action items that resulted from the meeting. Do not include speaker names unless it is important to differentiate.
"""}]
""",
}
],
)

print(response.message.content[0].text)
Expand Down
Loading

0 comments on commit b6700cc

Please sign in to comment.