Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add prompt tuning module #188

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions examples/conversation_prompt_tuning/main.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "initial_id",
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
80 changes: 80 additions & 0 deletions src/agentscope/prompt_tuning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from typing import List, Dict, Union

from agentscope.agents import DialogAgent, UserAgent, AgentBase
from agentscope.message import Msg

PROMPT_SYSTEM_PROMPT_GENERATION = (
"Given a task description, try to generate a system prompt for large "
"language models (LLMs). \n\n"
"## Note:\n"
"1. A system prompt should clearly describe the role that the LLM is "
"playing, its target, and notes."
"2. The system prompt should be concise and clear, and should not contain "
"any irrelevant information.\n\n"
"## Task Description:\n"
"{task_description}\n\n"
"## Generated System Prompt:\n"
)

PROMPT_FEW_SHOT_EXAMPLES = (
"## Example {index}:\n"
"Task description: {task_description}"
"Generated system prompt: {system_prompt}"
)


class PromptFactory(object):

def __init__(self, model_config_name: str) -> None:
"""Initialize a PromptFactory object."""
self.model_config_name = model_config_name

def generate_system_prompt(
self,
task_description: str,
few_shot_examples: List[Dict] = None,
interactive_mode: bool = False
) -> str:
"""Generate a system prompt for the given task description.

Args:
task_description (`str`):
The task description.
few_shot_examples (`List[Dict]`, defaults to `None`):
Few-shot examples for the system prompt generation.
interactive_mode (`bool`, defaults to `False`):
Whether to run in interactive mode.

Returns:
`str`: The generated system prompt.
"""

system_prompt = PROMPT_SYSTEM_PROMPT_GENERATION.format(task_description)
agent = DialogAgent(
"assistant",
sys_prompt=system_prompt,
model_config_name=self.model_config_name,
)
user = UserAgent("user")

msg = Msg(
"system",
PROMPT_SYSTEM_PROMPT_GENERATION.format(task_description),
"system"
)
while True:
msg = agent(msg)
# record the generated system prompt
generated_system_prompt = msg.content
if interactive_mode:
break

msg = user(msg)
if msg.content == "exit":
break

return generated_system_prompt

def tune_system_prompt(self, agent: Union[AgentBase, List[Msg]]):
"""Tune the system prompt according to the user's feedback."""
pass
Loading