Skip to content

Commit

Permalink
Init a prompt tuning module
Browse files Browse the repository at this point in the history
  • Loading branch information
DavdGao committed Apr 30, 2024
1 parent 5236126 commit c1d64a0
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
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

0 comments on commit c1d64a0

Please sign in to comment.