참고소스 수정본

This commit is contained in:
LASTA_DEV01\lasta
2026-05-12 19:40:31 +09:00
parent 0f34a451fc
commit 2e9204243d
8708 changed files with 3259488 additions and 869 deletions

View File

@@ -0,0 +1,67 @@
---
title: "Emotion Prompting"
description: "Adding phrases with emotional significance to humans can help enhance the performance of a language model."
---
Do language models respond to emotional stimuli?
Adding phrases with emotional significance to humans can help enhance the performance of a language model. This includes phrases such as:
- This is very important to my career.
- Take pride in your work.
- Are you sure?
!!! info
For more examples of emotional stimuli to use in prompts, look into [EmotionPrompt](https://arxiv.org/abs/2307.11760) -- a set of prompts inspired by well-established human psychological phenomena.
## Implementation
```python hl_lines="34"
import openai
import instructor
from pydantic import BaseModel
from typing import Iterable
class Album(BaseModel):
name: str
artist: str
year: int
client = instructor.from_provider("openai/gpt-5-nano")
def emotion_prompting(query, stimuli):
return client.create(
model="gpt-4o",
response_model=Iterable[Album],
messages=[
{
"role": "user",
"content": f"""
{query}
{stimuli}
""",
}
],
)
if __name__ == "__main__":
query = "Provide me with a list of 3 musical albums from the 2000s."
stimuli = "This is very important to my career." # (1)!
albums = emotion_prompting(query, stimuli)
for album in albums:
print(album)
#> name='Kid A' artist='Radiohead' year=2000
#> name='The Marshall Mathers LP' artist='Eminem' year=2000
#> name='The College Dropout' artist='Kanye West' year=2004
```
1. The phrase `This is very important to my career` is used as emotional stimuli in the prompt.
## References
<sup id="ref-1">1</sup>: [Large Language Models Understand and Can be Enhanced by Emotional Stimuli](https://arxiv.org/abs/2307.11760)

View File

@@ -0,0 +1,71 @@
---
description: "To help the model better infer human intention from ambigious prompts, we can ask the model to rephrase and respond (RaR)."
---
How can we identify and clarify ambigious information in the prompt?
Let's say we are given the query: *Was Ed Sheeran born on an odd month?*
There are many ways a model might interpret an *odd month*:
- Februray is *odd* because of an irregular number of days.
- A month is *odd* if it has an odd number of days.
- A month is *odd* if its numberical order in the year is odd (i.e. Janurary is the 1st month).
!!! note
Ambiguities might not always be so obvious!
To help the model better infer human intention from ambigious prompts, we can ask the model to rephrase and respond (RaR).
## Implementation
```python hl_lines="19"
from pydantic import BaseModel
import instructor
client = instructor.from_provider("openai/gpt-5-nano")
class Response(BaseModel):
rephrased_question: str
answer: str
def rephrase_and_respond(query):
return client.create(
model="gpt-4o",
messages=[
{
"role": "user",
"content": f"""{query}\nRephrase and expand the question, and respond.""", # (1)!
}
],
response_model=Response,
)
if __name__ == "__main__":
query = "Take the last letters of the words in 'Edgar Bob' and concatinate them."
response = rephrase_and_respond(query)
print(response.rephrased_question)
"""
What are the last letters of each word in the name 'Edgar Bob', and what do you get when you concatenate them?
"""
print(response.answer)
"""
To find the last letters of each word in the name 'Edgar Bob', we look at 'Edgar' and 'Bob'. The last letter of 'Edgar' is 'r' and the last letter of 'Bob' is 'b'. Concatenating these letters gives us 'rb'.
"""
```
1. This prompt template comes from [this](https://arxiv.org/abs/2311.04205) paper.
This can also be implemented as two-step RaR:
1. Ask the model to rephrase the question.
2. Pass the rephrased question back to the model to generate the final response.
## References
<sup id="ref-1">1</sup>: [Rephrase and Respond: Let Large Language Models Ask Better Questions for Themselves](https://arxiv.org/abs/2311.04205)

View File

@@ -0,0 +1,55 @@
---
description: "Re2 (Re-Reading) is a technique that asks the model to read the question again."
---
How can we enhance a model's understanding of a query?
Re2 (**Re** - **R** eading) is a technique that asks the model to read the question again.
!!! example "Re-Reading Prompting"
**Prompt Template**: Read the question again: <*query*> <*critical thinking prompt*><sup><a href="https://arxiv.org/abs/2309.06275">1</a></sup>
A common critical thinking prompt is: "Let's think step by step."
## Implementation
```python hl_lines="20"
import instructor
from pydantic import BaseModel
client = instructor.from_provider("openai/gpt-5-nano")
class Response(BaseModel):
answer: int
def re2(query, thinking_prompt):
return client.create(
model="gpt-4o",
response_model=Response,
messages=[
{
"role": "system",
"content": f"Read the question again: {query} {thinking_prompt}",
},
],
)
if __name__ == "__main__":
query = """Roger has 5 tennis balls.
He buys 2 more cans of tennis balls.
Each can has 3 tennis balls.
How many tennis balls does he have now?
"""
thinking_prompt = "Let's think step by step."
response = re2(query=query, thinking_prompt=thinking_prompt)
print(response.answer)
#> 11
```
## References
<sup id="ref-1">1</sup>: [Re-Reading Improves Reasoning in Large Language Models](https://arxiv.org/abs/2309.06275)

View File

@@ -0,0 +1,70 @@
---
title: "Role Prompting"
description: "Role prompting, or persona prompting, assigns a role to the model."
---
How can we increase a model's performance on open-ended tasks?
Role prompting, or persona prompting, assigns a role to the model. Roles can be:
- **specific to the query**: *You are a talented writer. Write me a poem.*
- **general/social**: *You are a helpful AI assistant. Write me a poem.*
## Implementation
```python hl_lines="27"
import openai
import instructor
from pydantic import BaseModel
client = instructor.from_provider("openai/gpt-5-nano")
class Response(BaseModel):
poem: str
def role_prompting(query, role):
return client.create(
model="gpt-4o",
response_model=Response,
messages=[
{
"role": "system",
"content": f"{role} {query}",
},
],
)
if __name__ == "__main__":
query = "Write me a short poem about coffee."
role = "You are a renowned poet."
response = role_prompting(query, role)
print(response.poem)
"""
In the morning's gentle light,
A brew of warmth, dark and bright.
Awakening dreams, so sweet,
In every sip, the day we greet.
Through the steam, stories spin,
A liquid muse, caffeine within.
Moments pause, thoughts unfold,
In coffee's embrace, we find our gold.
"""
```
!!! info "More Role Prompting"
To read about a systematic approach to choosing roles, check out [RoleLLM](https://arxiv.org/abs/2310.00746).
For more examples of social roles, check out [this](https://arxiv.org/abs/2311.10054) evaluation of social roles in system prompts..
To read about using more than one role, check out [Multi-Persona Self-Collaboration](https://arxiv.org/abs/2307.05300).
## References
<sup id="ref-1">1</sup>: [RoleLLM: Benchmarking, Eliciting, and Enhancing Role-Playing Abilities of Large Lanuage Models](https://arxiv.org/abs/2310.00746)
<sup id="ref-2">2</sup>: [Is "A Helpful Assistant" the Best Role for Large Language Models? A Systematic Evaluation of Social Roles in System Prompts ](https://arxiv.org/abs/2311.10054)
<sup id="ref-4">3</sup>: [Unleashing the Emergent Cognitive Synergy in Large Lanuage Models: A Task-Solving Agent through Multi-Persona Self-Collaboration ](https://arxiv.org/abs/2307.05300)

View File

@@ -0,0 +1,95 @@
---
title: "System 2 Attention (S2A)"
description: "The S2A (System 2 Attention) technique auto-refines a prompt by asking the model to rewrite the prompt to include only relevant information."
---
How do we remove irrelevant information from the prompt?
The S2A (System 2 Attention) technique auto-refines a prompt by asking the model to rewrite the prompt to include only *relevant* information. We implement this in two steps:
1. Ask the model to rewrite the prompt
2. Pass the rewritten prompt back to the model
## Implementation
```python hl_lines="25-28"
import openai
import instructor
from pydantic import BaseModel, Field
client = instructor.from_provider("openai/gpt-5-nano")
class Step1(BaseModel):
relevant_context: str = Field(..., description="Relevant context")
user_query: str = Field(..., description="The question from the user")
class Step2(BaseModel):
answer: int
def rewrite_prompt(query):
rewritten_prompt = client.create(
model="gpt-4o",
response_model=Step1,
messages=[
{
"role": "user",
"content": f"""
Given the following text by a user, extract the part
that is actually relevant to their question. Please
include the actual question or query that the user
is asking.
Text by user:
{query}
""", # (1)!
}
],
)
return rewritten_prompt
def generate_final_response(rewritten_prompt):
final_response = client.create(
model="gpt-4o",
response_model=Step2,
messages=[
{
"role": "user",
"content": f"""{rewritten_prompt.relevant_context}
Question: {rewritten_prompt.user_query}""",
}
],
)
return final_response
if __name__ == "__main__":
query = """Mary has 3 times as much candy as Megan.
Mary then adds 10 more pieces of candy to her collection.
Max is 5 years older than Mary.
If Megan has 5 pieces of candy, how many does Mary have in total?
"""
# Step 1: Rewrite the prompt
rewritten_prompt = rewrite_prompt(query)
print(rewritten_prompt.relevant_context)
"""
Mary has 3 times as much candy as Megan. Mary then adds 10 more pieces of candy to her collection. If Megan has 5 pieces of candy, how many does Mary have in total?
"""
print(rewritten_prompt.user_query)
#> how many does Mary have in total?
# Step 2: Generate the final response
final_response = generate_final_response(rewritten_prompt)
print(final_response.answer)
#> 25
```
1. This prompt template comes from [this](https://arxiv.org/abs/2311.11829) paper.
## References
<sup id="ref-1">1</sup>: [System 2 Attention (is something you might need too)](https://arxiv.org/abs/2311.11829)

View File

@@ -0,0 +1,77 @@
---
title: "Self-Ask"
description: "Self-Ask is a technique which use a single prompt to encourage a model to use the answers to sub-problems to correctly generate the overall solution."
---
Models can sometimes correctly answer sub-problems but incorrectly answer the overall query. This is known as the *compositionality gap*<sup><a href="https://arxiv.org/abs/2210.03350">1</a></sup>.
How can we encourage a model to use the answers to sub-problems to correctly generate the overall solution?
Self-Ask is a technique which use a single prompt to:
- decide if follow-up questions are required
- generate the follow-up questions
- answer the follow-up questions
- answer the main query
## Implementation
```python hl_lines="26-29"
import instructor
from pydantic import BaseModel, Field
client = instructor.from_provider("openai/gpt-5-nano")
class FollowUp(BaseModel):
question: str = Field(description="The follow-up question")
answer: str = Field(description="The answer to the follow-up question")
class Response(BaseModel):
follow_ups_required: bool
follow_ups: list[FollowUp]
final_answer: str
def self_ask(query):
return client.create(
model="gpt-4o",
response_model=Response,
messages=[
{
"role": "system",
"content": f"""Query: {query}
Are follow-up questions needed?
If so, generate follow-up questions, their answers, and then the final answer to the query.
""", # !(1)
},
],
)
if __name__ == "__main__":
query = "Who was president of the U.S. when superconductivity was discovered?"
response = self_ask(query)
print(response.follow_ups_required)
#> True
for follow_up in response.follow_ups:
print(follow_up)
"""
question='When was superconductivity discovered?' answer='Superconductivity was discovered in April 1911.'
"""
"""
question='Who was president of the U.S. in April 1911?' answer='William Howard Taft was the President of the United States in April 1911.'
"""
print(response.final_answer)
"""
William Howard Taft was president of the U.S. when superconductivity was discovered.
"""
```
1. Without `instructor`, this prompt would generally be implemented as a one-shot or few-shot prompt<sup><a href="https://arxiv.org/abs/2210.03350">1</a></sup> to encourage thinking through follow-up questions. With `instructor`, we use a zero-shot prompt!
## References
<sup id="ref-1">1</sup>: [Measuring and Narrowing the Compositionality Gap in Language Models](https://arxiv.org/abs/2210.03350)

View File

@@ -0,0 +1,103 @@
---
title: "SimToM (Simulated Theory of Mind)"
description: "SimToM (Simulated Theory of Mind) is a two-step prompting technique that encourages a model to consider a specific perspective."
---
How can we encourage the model to focus on relevant information?
SimToM (Simulated Theory of Mind) is a two-step prompting technique that encourages a model to consider a specific perspective.
This can be useful for complex questions with multiple entities. For example, if the prompt contains information about two individuals, we can ask the model to answer our query from the perspective of one of the individuals.
This is implemented in two steps. Given an entity:
1. Identify and isolate information relevant to the entity
2. Ask the model to answer the query from the entity's perspective
!!! example "Sample Template"
**Step 1**: Given the following context, list the facts that <*entity*> would know. Context: <*context*>
**Step 2**: You are <*entity*>. Answer the following question based only on these facts you know: <*facts*>. Question: <*query*>
## Implementation
```python hl_lines="24-25"
import openai
import instructor
from pydantic import BaseModel, Field
from typing import Iterable
client = instructor.from_provider("openai/gpt-5-nano")
class KnownFact(BaseModel):
fact: str = Field(description="A fact that the given entity would know")
class Response(BaseModel):
location: str
def generate_known_facts(entity, context, query) -> Iterable[KnownFact]:
return client.create(
model="gpt-4o",
response_model=Iterable[KnownFact],
messages=[
{
"role": "user",
"content": f"""Given the following context, list
the facts that {entity} would know:
Context:
{context}
{query}
List only the facts relevant to {entity}.
""",
}
],
)
def answer_question_based_on_facts(entity, query, known_facts) -> Response:
return client.create(
model="gpt-4o",
response_model=Response,
messages=[
{
"role": "system",
"content": f"""You are {entity}. Answer the following question
based only on these facts you know:
{" ".join([str(fact) for fact in known_facts])}""",
},
{
"role": "user",
"content": f"Question: {query}",
},
],
)
if __name__ == "__main__":
entity = "Alice"
context = """Alice puts the book on the table.
Alice leaves the room.
Bob moves the book to the shelf.
"""
query = f"Where does {entity} think the book is?"
known_facts = generate_known_facts(entity, context, query)
response = answer_question_based_on_facts(entity, query, known_facts)
for fact in known_facts:
print(fact)
#> fact='Alice puts the book on the table.'
#> fact='Alice leaves the room. Bob moves the book to the shelf.'
print(response.location)
#> On the table
```
## References
<sup id="ref-1">1</sup>: [Think Twice: Perspective-Taking Improves Large Language Models' Theory-of-Mind Capabilities](https://arxiv.org/abs/2311.10227)

View File

@@ -0,0 +1,91 @@
---
title: "Style Prompting"
description: "To contrain a model's response to fit the boundaries of our task, we can specify a style."
---
How can we constrain model outputs through prompting alone?
To contrain a model's response to fit the boundaries of our task, we can specify a style.
Stylistic constraints can include:
- **writing style**: write a *flowery* poem
- **tone**: write a *dramatic* poem
- **mood**: write a *happy* poem
- **genre**: write a *mystery* poem
## Implementation
```python hl_lines="22"
import instructor
from pydantic import BaseModel
import openai
class Email(BaseModel):
subject: str
message: str
client = instructor.from_provider("openai/gpt-5-nano")
def generate_email(subject, to, sender, tone):
return client.create(
model="gpt-4o",
messages=[
{
"role": "user",
"content": f"""
Write an email about {subject} to {to} from {sender}.
The email should be {tone}.
""",
}
],
response_model=Email,
)
if __name__ == "__main__":
email = generate_email(
subject="invitation to all-hands on Monday at 6pm",
to="John Smith",
sender="Jane Doe",
tone="formal",
)
print(email.subject)
#> Invitation to All-Hands Meeting
print(email.message)
"""
Dear Mr. Smith,
I hope this message finds you well. I am writing to formally invite you to our upcoming all-hands meeting scheduled for Monday at 6:00 PM. This meeting is an important opportunity for us to come together, discuss key updates, and align on our strategic goals.
Please confirm your availability at your earliest convenience. Your presence and contributions to the discussion would be greatly valued.
Thank you and I look forward to your confirmation.
Warm regards,
Jane Doe
"""
```
## Stylistic Constraint Examples
| Constraint | Possible Phrases |
|----------------|-----------------------------------------------------------------------------------|
| Writing Style | Functional, Flowery, Candid, Prosaic, Ornate, Poetic |
| Tone | Dramatic, Humorous, Optimistic, Sad, Formal, Informal |
| Mood | Angry, Fearful, Happy, Sad |
| Genre | Historical Fiction, Literary Fiction, Science Fiction, Mystery, Dystopian, Horror |
!!! info "More Stylistic Constraints"
To see even more examples of these stylistic constraints and additional constraints (**characterization**, **pacing**, and **plot**), check out [this](https://arxiv.org/abs/2302.09185) paper.
## References
<sup id="ref-1">1</sup>: [Bounding the Capabilities of Large Language Models in Open Text Generation with Prompt Constraints](https://arxiv.org/abs/2302.09185)