Skip to content

Commit

Permalink
Stylized responses (aka responses with custom system prompt).
Browse files Browse the repository at this point in the history
  • Loading branch information
j-durbin committed Aug 10, 2023
1 parent 0b71528 commit c0eb33a
Show file tree
Hide file tree
Showing 26 changed files with 249 additions and 46 deletions.
4 changes: 2 additions & 2 deletions airoboros/instructors/agent.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from airoboros.instructors.inline_qa import generate as generate_inline


async def generate(instructor):
async def generate(instructor, **kwargs):
"""Generator for agent/router training data."""
async for item in generate_inline(
instructor, "agent", start_key="PROMPT", filter_response=False
instructor, "agent", start_key="PROMPT", filter_response=False, **kwargs
):
yield item
4 changes: 2 additions & 2 deletions airoboros/instructors/card.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from airoboros.instructors.inline_qa import generate as generate_inline


async def generate(instructor):
async def generate(instructor, **kwargs):
"""Generator for model/scenario card training data."""
async for item in generate_inline(instructor, "card", start_key="PROMPT"):
async for item in generate_inline(instructor, "card", start_key="PROMPT", **kwargs):
yield item
3 changes: 2 additions & 1 deletion airoboros/instructors/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ async def generate_cards(instructor):
logger.success(f"Generated chat character card {filename}")
if len(cards) >= card_count:
break
instructor.instructor_counts["chat_card"] = len(cards)
return cards


Expand Down Expand Up @@ -427,7 +428,7 @@ async def generate_chat(instructor, cards, topic, **api_params):
return training


async def generate(instructor):
async def generate(instructor, **kwargs):
"""Generator for chat training data."""
config = instructor.instructors.get("chat", {})
if not config:
Expand Down
2 changes: 1 addition & 1 deletion airoboros/instructors/coding.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import random


async def generate(instructor):
async def generate(instructor, **kwargs):
"""Generator for coding training data."""
config = instructor.instructors.get("coding")
if not config:
Expand Down
2 changes: 1 addition & 1 deletion airoboros/instructors/contextual.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def generate_prompt(instructor, config, template, topic_iter):
)


async def generate(instructor):
async def generate(instructor, **kwargs):
"""Generator for contextual training data."""
config = instructor.instructors.get("contextual")
if not config:
Expand Down
6 changes: 4 additions & 2 deletions airoboros/instructors/cot.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from airoboros.instructors.simple_task import generate as generate_simple


async def generate(instructor):
async def generate(instructor, **kwargs):
"""Generator for chain-of-thought training data."""
async for item in generate_simple(instructor, "cot", filter_response=False):
async for item in generate_simple(
instructor, "cot", filter_response=False, **kwargs
):
yield item
2 changes: 1 addition & 1 deletion airoboros/instructors/counterfactual_contextual.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
]


async def generate(instructor):
async def generate(instructor, **kwargs):
"""Generator for contextual training data."""
config = instructor.instructors.get("counterfactual_contextual")
if not config:
Expand Down
5 changes: 2 additions & 3 deletions airoboros/instructors/detailed_writing.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
)


async def generate(instructor):
async def generate(instructor, **kwargs):
"""Generator for detailed writing training data."""
config = instructor.instructors.get("detailed_writing", {})
if not config:
Expand Down Expand Up @@ -60,8 +60,7 @@ async def generate(instructor):

# Load the topics.
topics = instructor.get_instructor_topics(config)
random.shuffle(topics)
topic_index = 0
topic_index = random.randint(0, len(topics) - 1)

# API params, overriding defaults with this instructor's config.
api_params = {**instructor.api_params, **config.get("api_params", {})}
Expand Down
3 changes: 2 additions & 1 deletion airoboros/instructors/experience.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re


async def generate(instructor):
async def generate(instructor, **kwargs):
"""Generator for experiences."""
config = instructor.instructors.get("experience")
if not config:
Expand Down Expand Up @@ -43,6 +43,7 @@ async def generate(instructor):
futures.append(
instructor.generate_response(
prompt.format(language=language, flesch=flesch),
messages=kwargs.get("messages", []),
filter_response=False,
**api_params
)
Expand Down
13 changes: 8 additions & 5 deletions airoboros/instructors/general.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import asyncio
import json
import os
import random
import re


async def generate(instructor):
async def generate(instructor, **kwargs):
"""Generator for generic/general training data."""
config = instructor.instructors.get("general")
if not config:
Expand All @@ -25,9 +26,7 @@ async def generate(instructor):

# Load the topics.
topics = instructor.get_instructor_topics(config)
topic_index = instructor.instructor_counts.get("general", 0)
if topic_index >= len(topics):
topic_index = topic_index % len(topics)
topic_index = random.randint(0, len(topics) - 1)

# API params, overriding defaults with this instructor's config.
api_params = {**instructor.api_params, **config.get("api_params", {})}
Expand Down Expand Up @@ -85,7 +84,11 @@ async def generate(instructor):
):
continue
instructions.append(instruction)
futures.append(instructor.generate_response(instruction, **api_params))
futures.append(
instructor.generate_response(
instruction, messages=kwargs.get("messages", []), **api_params
)
)
if not futures:
continue
responses = await asyncio.gather(*futures)
Expand Down
11 changes: 7 additions & 4 deletions airoboros/instructors/inline_qa.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
import random
import re


Expand All @@ -10,6 +11,7 @@ async def generate(
end_key="ANSWER",
filter_response=True,
template_kwargs={},
**kwargs,
):
"""Generator for generic inline question answer pair training data."""
config = instructor.instructors.get(category)
Expand All @@ -31,9 +33,7 @@ async def generate(

# Topics included in template?
topics = instructor.get_instructor_topics(config)
topic_index = instructor.instructor_counts.get(category, 0)
if topic_index >= len(topics):
topic_index = topic_index % len(topics)
topic_index = random.randint(0, len(topics) - 1)

# API params, overriding defaults with this instructor's config.
api_params = {**instructor.api_params, **config.get("api_params", {})}
Expand Down Expand Up @@ -83,7 +83,10 @@ async def generate(

prompt = template.format(**prompt_args)
response = await instructor.generate_response(
prompt, filter_response=filter_response, **api_params
prompt,
messages=kwargs.get("messages", []),
filter_response=filter_response,
**api_params,
)
if not response:
continue
Expand Down
4 changes: 2 additions & 2 deletions airoboros/instructors/joke.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from airoboros.instructors.inline_qa import generate as generate_inline


async def generate(instructor):
async def generate(instructor, **kwargs):
"""Generator for joke training data."""
async for item in generate_inline(instructor, "joke"):
async for item in generate_inline(instructor, "joke", **kwargs):
yield item
2 changes: 1 addition & 1 deletion airoboros/instructors/multiple_choice.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
)


async def generate(instructor):
async def generate(instructor, **kwargs):
"""Generator for multiple choice training data."""
config = instructor.instructors.get("multiple_choice")
if not config:
Expand Down
4 changes: 2 additions & 2 deletions airoboros/instructors/orca.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from airoboros.instructors.inline_qa import generate as generate_inline


async def generate(instructor):
async def generate(instructor, **kwargs):
"""Generator for orca training data."""
async for item in generate_inline(instructor, "orca"):
async for item in generate_inline(instructor, "orca", **kwargs):
yield item
6 changes: 4 additions & 2 deletions airoboros/instructors/plan.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from airoboros.instructors.simple_task import generate as generate_simple


async def generate(instructor):
async def generate(instructor, **kwargs):
"""Generator for rewoo style planning."""
async for item in generate_simple(instructor, "plan", filter_response=False):
async for item in generate_simple(
instructor, "plan", filter_response=False, **kwargs
):
yield item
4 changes: 2 additions & 2 deletions airoboros/instructors/riddle.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from airoboros.instructors.inline_qa import generate as generate_inline


async def generate(instructor):
async def generate(instructor, **kwargs):
"""Generator for riddle training data."""
async for item in generate_inline(instructor, "riddle"):
async for item in generate_inline(instructor, "riddle", **kwargs):
yield item
4 changes: 2 additions & 2 deletions airoboros/instructors/roleplay.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from airoboros.instructors.simple_task import generate as generate_simple_task


async def generate(instructor):
async def generate(instructor, **kwargs):
"""Generator for roleplay training data."""
async for item in generate_simple_task(instructor, "roleplay"):
async for item in generate_simple_task(instructor, "roleplay", **kwargs):
yield item
27 changes: 26 additions & 1 deletion airoboros/instructors/simple_task.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import asyncio
import json
import os
import random
import re


Expand All @@ -9,6 +11,7 @@ async def generate(
filter_response=True,
only_instructions=False,
template_kwargs={},
**kwargs,
):
"""Generator for simple instruction response tasks (e.g. roleplay, wordgames)."""
config = instructor.instructors.get(category)
Expand Down Expand Up @@ -46,6 +49,10 @@ async def generate(
min_score = instructor.min_docsearch_score
min_score = float(min_score)

# Load the topics.
topics = instructor.get_instructor_topics(config)
topic_index = random.randint(0, len(topics) - 1)

# Generate the instruction/response pairs until we reach the target count.
batch_size = config.get("batch_size")
if batch_size is None:
Expand All @@ -63,6 +70,21 @@ async def generate(
format_args[key] = val(instructor)
if "{topic_avoidance}" in template:
format_args["topic_avoidance"] = instructor.topic_avoidance
if "{topics}" in template:
# Inject the topics to use for this batch.
current_topics = []
for _ in range(batch_size):
current_topics.append(topics[topic_index])
topic_index += 1
if topic_index >= len(topics):
topic_index = 0
topics_str = "\n".join(
[
f" * TSK {idx + 1} must be related to topic: {json.dumps(topic)}"
for idx, topic in enumerate(current_topics)
]
)
format_args["topics"] = topics_str

# Get a batch of instructions.
prompt = template.format(**format_args)
Expand Down Expand Up @@ -93,7 +115,10 @@ async def generate(
)
futures.append(
instructor.generate_response(
full_prompt, filter_response=filter_response, **api_params
full_prompt,
messages=kwargs.get("messages", []),
filter_response=filter_response,
**api_params,
)
)
if not futures:
Expand Down
4 changes: 2 additions & 2 deletions airoboros/instructors/song.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from airoboros.instructors.simple_task import generate as generate_simple_task


async def generate(instructor):
async def generate(instructor, **kwargs):
"""Generator for song training data."""
async for item in generate_simple_task(instructor, "song"):
async for item in generate_simple_task(instructor, "song", **kwargs):
yield item
Loading

0 comments on commit c0eb33a

Please sign in to comment.