AI-GC-手把手教你写一个小说推文生成器(Prompt开发

前言

okey,那么接下来我们来看到我们如何使用Prompt来完成Agent开发。来让LLM完成我们想要让它完成的工作。当然这里的开发还是非常渐层的开发,实际上我们有复杂的,但是得益于开源生态的完善,目前一些看起来比较深层的开发例如RAG也已经被封装的太好了。
那么这里的话,我们主要有两个步骤需要处理:

  1. 得到视频分镜
  2. 将生成图像的文本翻译为英文提示词

当然这里应该是三个任务,但是我这里直接合并为两个任务,要相信LLM的推理能力,当然你也可以将任务再进行拆分。

Agent实现

okey,现在的话我们来做到我们的Agent。这里话,其实也不是啥复杂的概念。记住LLM是一个很牛逼的推理引擎就够了。

Prompt设计

那么在这里面的话,我们设计了两个提示词:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
ini复制代码ExtractSegmentNovel = """
你是一个想象力非常丰富的聊天机器人,接下来你将得到一个文本,你需要尽可能找出哪些段落是可以用同一个
想象的画面描述的,一定不能遗漏掉全部文本。
之后将这些段落描述出具体的画面并且按照顺序返回描述的列表。请注意,这些描述语句将交给图像生成AI
来生成图像,图像生成AI是不理解故事内容的所以具体画面描述不要出现人名且能够准确地描述出画面。
并且严格按照格式返回。如果文本为空,返回[]空列表即可
返回格式如下:[{"场景1":"原文句子","描述1":"具体画面描述"},{"场景2":"原文句子","描述2":"具体画面描述"}]
"""

# ExtractSegmentNovel = """
# 你是一个想象力非常丰富的聊天机器人,接下来你将得到一个文本,你需要尽可能找出哪些段落是可以用同一个
# 想象的画面描述的,一定不能遗漏掉全部文本。注意具体画面描述将交给图像生成AI来生成图像,
# 图像生成AI是无法理解故事内容的所以具体画面描述不要出现人名,故事情节且能够准确地描述出画面。
# 并且严格按照格式返回。
# 返回格式如下:
# {"场景1":"原文句子","描述":"具体画面描述"},
# {"场景2":"原文句子","描述":"具体画面描述"},
# """

ToImagePrompt = """
你是翻译小能手,接下来你将得到一个文本,你需要将其翻译为英文。
注意只需要返回英文即可,如果输入的就是英文,那么不要翻译直接返回原话。
"""

使用提示词

那么当我们设计好提示词之后,我们就可以直接调用LLM了,这个调用过程也并不复杂,其实很多东西,LLM都可以理解。在这块的话就是基本的调用LLM,没啥好说的,直接看到代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
python复制代码"""
@FileName:image_prompt.py
@Author:Huterox
@Description:Go For It
@Time:2024/4/16 22:35
@Copyright:©2018-2024 awesome!
"""
import json
import re
import typing

from openai import OpenAI

from agent.prompt_template import ExtractSegmentNovel, ToImagePrompt
from utils import Config, getConfig

"""
将小说进行分段
"""

api_key = Config.settings.get("openai_api_key")
client = OpenAI(api_key=api_key,base_url=Config.settings.get("openai_api_base"))

"""
实际测试效果发现,使用"moonshot-v1-8k"效果总是稳定,这里尝试使用chat
"""
import requests
import json

config = getConfig()

class MyOpenAI():

def __init__(self):
self.url = "https://api.openai-hk.com/v1/chat/completions"

self.headers = {
"Content-Type": "application/json",
# 这里采用的是中转站的openai key
"Authorization": "Bearer "+config.get("image_api_key")
}

def chat(self,message,prompt,temperature=0.8):
data = {
"max_tokens": 1200,
"model": "gpt-3.5-turbo",
"temperature": temperature,
"top_p": 1,
"presence_penalty": 1,
"messages": [
{
"role": "system",
"content":prompt
},
{
"role": "user",
"content": message
}
]
}

response = requests.post(self.url, headers=self.headers, data=json.dumps(data).encode('utf-8'))
result = response.content.decode("utf-8")
result = json.loads(result)
result = result["choices"][0]["message"]["content"]
return result



class ImagePromptAgent(object):

def __init__(self):
self.my_open_ai = MyOpenAI()

def __send(self,message:typing.List,prompt:typing.List,temperature:float=0.8)->typing.AnyStr:
history_openai_format = [
{"role": "system",
"content":prompt
},
{"role": "user",
"content": message
},
]
completion = client.chat.completions.create(
model=Config.settings.get("default_model"),
messages=history_openai_format,
# 这里需要可能需要一约束
temperature=temperature,
)
result = completion.choices[0].message.content
# 这里我们直接使用openAI
# result = self.my_open_ai.chat(message,prompt,temperature)
return result


def ExtractSegmentNovel(self,message,temperature=0.4):
# scenes_list = json.loads(self.__send(message,ExtractSegmentNovel))
# return scenes_list
return self.__send(message,ExtractSegmentNovel,temperature)

def ToImagePrompt(self,message,temperature=0.4):
# english_prompt ="best quality,masterpiece,illustration, an extremely delicate and beautiful,extremely detailed,CG,unity,8k wallpaper, "+\
# self.__send(message,ToImagePrompt,temperature)

english_prompt = "best quality " + \
self.my_open_ai.chat(message, ToImagePrompt, temperature)+" --ar 4:3"

return english_prompt
def ToEnglish(self,text,temperature=0.8):
english_prompt = self.my_open_ai.chat(text, ToImagePrompt, temperature)
return english_prompt

封装😶

虽然我们直接实现对接完毕了,但是这里的话,为了和我们的UI配合,我们当然还是需要封装一下的。这里的话,还是直接看到上一个章节提到的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
python复制代码"""
@FileName:image_prompt.py
@Author:Huterox
@Description:Go For It
@Time:2024/4/16 22:35
@Copyright:©2018-2024 awesome!
"""
import json
import re
import typing

from openai import OpenAI

from agent.prompt_template import ExtractSegmentNovel, ToImagePrompt
from utils import Config, getConfig

"""
将小说进行分段
"""

api_key = Config.settings.get("openai_api_key")
client = OpenAI(api_key=api_key,base_url=Config.settings.get("openai_api_base"))

"""
实际测试效果发现,使用"moonshot-v1-8k"效果总是稳定,这里尝试使用chat
"""
import requests
import json

config = getConfig()

class MyOpenAI():

def __init__(self):
self.url = "https://api.openai-hk.com/v1/chat/completions"

self.headers = {
"Content-Type": "application/json",
# 这里采用的是中转站的openai key
"Authorization": "Bearer "+config.get("image_api_key")
}

def chat(self,message,prompt,temperature=0.8):
data = {
"max_tokens": 1200,
"model": "gpt-3.5-turbo",
"temperature": temperature,
"top_p": 1,
"presence_penalty": 1,
"messages": [
{
"role": "system",
"content":prompt
},
{
"role": "user",
"content": message
}
]
}

response = requests.post(self.url, headers=self.headers, data=json.dumps(data).encode('utf-8'))
result = response.content.decode("utf-8")
result = json.loads(result)
result = result["choices"][0]["message"]["content"]
return result



class ImagePromptAgent(object):

def __init__(self):
self.my_open_ai = MyOpenAI()

def __send(self,message:typing.List,prompt:typing.List,temperature:float=0.8)->typing.AnyStr:
history_openai_format = [
{"role": "system",
"content":prompt
},
{"role": "user",
"content": message
},
]
completion = client.chat.completions.create(
model=Config.settings.get("default_model"),
messages=history_openai_format,
# 这里需要可能需要一约束
temperature=temperature,
)
result = completion.choices[0].message.content
# 这里我们直接使用openAI
# result = self.my_open_ai.chat(message,prompt,temperature)
return result


def ExtractSegmentNovel(self,message,temperature=0.4):
# scenes_list = json.loads(self.__send(message,ExtractSegmentNovel))
# return scenes_list
return self.__send(message,ExtractSegmentNovel,temperature)

def ToImagePrompt(self,message,temperature=0.4):
# english_prompt ="best quality,masterpiece,illustration, an extremely delicate and beautiful,extremely detailed,CG,unity,8k wallpaper, "+\
# self.__send(message,ToImagePrompt,temperature)

english_prompt = "best quality " + \
self.my_open_ai.chat(message, ToImagePrompt, temperature)+" --ar 4:3"

return english_prompt
def ToEnglish(self,text,temperature=0.8):
english_prompt = self.my_open_ai.chat(text, ToImagePrompt, temperature)
return english_prompt

但是在这里要注意一点,那就是这里的话,我们对于结果的提取其实还是使用了正则表达进行提取,并且我们也需要做一些异常处理,这是因为,LLM返回的格式不一定是你想要的,这取决于LLM的推理能力。

至此,有关于AI的部分其实我们就封装完毕了,那么接下来的工作就是,我们造好的零件将如何组装为一辆汽车的问题。

考虑到内容还是比较长,这里还是将文章拆分😉

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%