您准备好探索生成AI的奇妙世界,创作出富有吸引力的故事并配以引人入胜的插图了吗?根据我最近使用GPT-4和Midjourney的体验,我成功地使用YiVal构建了一个端到端的叙事,GPT-4用来生成大规模的故事,同时Midjourney创建令人惊叹的插图。
1.定义用例
在本教程中,我们将重点介绍如何构建一个应用程序,该应用程序将有关动物物种和性格的信息作为输入,并生成引人入胜的故事作为输出。为了增加额外的沉浸感,让我们通过使用 Midjourney 创建随附的图像来增强生成的故事——Midjourney 是一种将故事转化为精美插图的出色工具。
2.手动工作流程
一种简单的方法是编写一个简单的提示,例如“作为一名讲故事的人,给我讲一个关于勇敢狮子的动物故事”。然而,使用这种方法,LLM(大型语言模型)的输出通常不能令人满意。此外,为Midjourney调整提示的过程非常耗时。一些博主声称他们可以使用GPT-4和Midjourney在几分钟内生成高质量的叙述。相比之下,我花了15分钟才生成一个故事,还不包括设置时间。调整提示和在GPT-4和Midjourney之间切换非常耗时和痛苦。
多轮故事提示调整
生成的可怕图片
所以后来,我特意加了一个故事提示,确保叙事不吓人,从而开始了新一轮的调优......
3.YiVal 工作流程
让我们看看 YiVal 是如何简化这个问题的。YiVal github仓库中有一个demo。 下面是一个YiVal的工作流程,根据动物的种类和性格编写故事,然后中途会生成这个故事的相应封面。
opanai_prompt_data_generator会生成动物物种及其性格,如{cat , brave}
变体(variation)是指原始提示的不同版本或改编,同时仍保持核心目标 - 生成动物故事。在这个demo中,我们编写了一个提示generate-animal-story-template。变体在实验中至关重要,它们能够为同一任务提供动态的视角和方法。
我们用 { species , character } 格式化这个模板,并通过 GPT-4 生成详细的故事,并使用 { drawing_style } 作为 Midjourney 的输入来可视化这个故事。生成的故事和动物图像将由人类评估员进行评估,用户可以轻松地对生成的结果进行反馈。
您可以使用或遵循YiVal github仓库中的demo notebook来运行demo。
!poetry run yival run /content/YiVal/demo/configs/animal_story.yml
您将能够在端口号 127.0.0.1:8073
中看到结果。
数据生成
我使用基于 GPT-4 的自动数据集生成来辅助生成 20 个随机animal_spieces和animal_charator数据集。这将帮助我们评估提示的性能,并提供用于评估的数据。
dataset:
data_generators:
openai_prompt_data_generator:
chunk_size: 2
diversify: true
prompt:
"Please provide a concrete and realistic test case as a dictionary for function invocation using the ** operator.
Only include parameters, excluding description and name.
Ensure it's succinct and well-structured.
**Only provide the dictionary.**"
input_function:
description:
Given the species of an animal and its character, generate a corresponding story
name: animal_story_generation
parameters:
species: str
character: str
drawing_style: str
number_of_examples: 20
model_name: gpt-4
output_path: animal_story.pkl
source_type: machine_generated
提示创建组合
variations:
- name: task
generator_name: openai_prompt_based_variation_generator
generator_config:
model_name: gpt-4
number_of_variations: 3
diversify: true
max_tokens: 2000
variables: null
prompt:
- content: |-
Your object is to construct a concise instruction prompt for GPT-4. This prompt will instruct GPT-4 as a gentle, imaginative children's writer to write all kinds of cute, kid-friendly stories based on animal species and personalities for an audience of YOUNG ADULTS and TEENAGERS.
Points to emphasize in your instruction:
- GPT4 Answers should have a short storyline and should be sufficiently imaginative, answers should focus on the location in which the story takes place with its style of drawing, the location and style of drawing can be contrary to the laws of reality, dream-like or realistic.
- The story must be kept lively and as short, short, short, short, short as possible. The story should have a variety of backgrounds + drawing styles and fully emphasize the location where the story takes place; it can be dreamlike and generally bizarre.
- Be sure to use words that are as easy to understand as possible so TEENAGERS can easily understand the story.
- Please limit the output to within 300 words
- {animal_species} represent the species of the animal
- {animal_character} represent the character of the animal
- At all times, gpt-4 must give a response as an animal story
Craft your instruction Ensure that GPT-4 is able to fully understand the types and personalities of small animals and write engaging and fully pictorial stories.
keep your output crisp: only the prompt, devoid of any extraneous content.
role: system
custom_function: demo.animal_story.image_generation
description: Generated experiment config
evaluators:
- evaluator_type: individual
metric_calculators:
- method: AVERAGE
name: openai_prompt_based_evaluator
display_name: story_quality
prompt: |-
You are assessing a submitted story based on a given task based on a criterion. Here is the data:
- Task: Given the species of an animal and corresponding character, generate one story.
- Does the story demonstrate creativity, coherence, and engaging narrative? A well-written story should captivate the reader and effectively convey a compelling narrative with clear structure and character development.
[Input]: animal_species : {species}, animal_character: {character}
[Result]: {raw_output}
Answer the question by selecting one of the following options:
A The story is of very low quality and lacks coherence and engagement.
B The story has some issues with coherence and engagement, but has potential for improvement.
C The story is of satisfactory quality, but there is room for improvement.
D The story is well-written and demonstrates good coherence and engagement.
E The story is exceptionally well-written, demonstrating strong coherence and engagement.
choices: ["A", "B", "C", "D", "E"]
# model_name specify the llm model , e.g. a16z-infra/llama-2-13b-chat:9dff94b1bed5af738655d4a7cbcdcde2bd503aa85c94334fe1f42af7f3dd5ee3
model_name: gpt-4
description: "evaluate the quality of the generated story"
scale_description: "0-4"
choice_scores:
A: 0
B: 1
C: 2
D: 3
E: 4
human_rating_configs:
- name: relevance
instructions: Rate the relevance of the image to the story.
scale: [1, 5]
- name: image_quality
instructions: Rate the quality of the generated image.
scale: [1, 5]
4.人工输入进行评估
YiVal 通过提供强大的评估器功能来确保生成故事的质量和相关性。YiVal 中的评估器支持自动评估,包括延迟度和令牌数量等指标。
YiVal 通过提供用户友好的人工标签 UI 进一步增强了评估过程。开发人员可以轻松提供人工评估,添加关键的人工反馈层来完善和微调生成的故事,使生成的故事能够与受众产生共鸣。
单击“人工评分(human rating)”列下的链接即可跳转到评分仪表板。例如,我们可以根据相关性、图片质量等对动物故事进行评分,并将人类标签保存为评估。
5.结语
我们已经了解了 YiVal 如何加速开发和评估过程,现在一起来创建我们的动物故事应用程序吧!通过使用 YiVal来生成提示、评估结果以及结合人工反馈,我们打造出了引人入胜的故事,并使用 Midjourney将这些故事转化为栩栩如生的插图,让故事更加鲜活。?