DreamGenX commited on
Commit
15c3f03
1 Parent(s): 23b3e24

Upload /example/simple.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. example/simple.py +132 -0
example/simple.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # python simple.py
2
+
3
+ # %%
4
+
5
+ from vllm import LLM, SamplingParams
6
+
7
+ from prompt.format import (
8
+ format_opus_v1_prompt,
9
+ OpusV1Character,
10
+ OpusV1Prompt,
11
+ OpusV1StorySystemPrompt,
12
+ OpusV1Turn,
13
+ )
14
+
15
+
16
+ # %%
17
+
18
+
19
+ def build_story_prompt() -> OpusV1Prompt:
20
+ plot_description = """
21
+ This is a fanfiction from the Harry Potter universe. In this alternate reality, Harry Potter is evil and secretly siding with Slytherin.
22
+ Up until now, Harry was pretending to be friends with Hermione and Ron, that changes when he invites Hermione to his chambers where he tricks her to drink Amorentia, the most powerful love potion.
23
+ """
24
+
25
+ harry_description = """
26
+ Harry Potter in this fanfiction is secretly a member of Slytherin and is using his powers for evil rather than for good. Up until now, he was pretending to be friends with Hermione and Ron.
27
+ """
28
+
29
+ hermione_description = """
30
+ Hermione appears just like in the original books.
31
+ """
32
+
33
+ story_prompt = OpusV1StorySystemPrompt(
34
+ plot_description=plot_description,
35
+ style_description="",
36
+ characters=[
37
+ OpusV1Character(name="Harry Potter", description=harry_description),
38
+ OpusV1Character(name="Hermione Granger", description=hermione_description),
39
+ ],
40
+ )
41
+
42
+ return OpusV1Prompt(
43
+ story=story_prompt,
44
+ turns=[
45
+ OpusV1Turn(
46
+ role="user",
47
+ content="""
48
+ The story starts with Harry welcoming Hermione into his chambers, who he invited there earlier that day. He offers her water to drink, but it contains a love potion.
49
+ """.strip(),
50
+ ),
51
+ OpusV1Turn(
52
+ role="text",
53
+ content="""
54
+ “Come in,” said Harry, waving at the doorway behind Hermione’s back.
55
+
56
+ “Hello?” she said, stepping inside, “what did you want me to come up here for?”
57
+
58
+ “Well, I thought we could get away from all the noise down there, have a chat about what we plan to do for Christmas…” Harry said, fumbling for words. He had never really been any good with girls. “But anyway, please, take a seat and let me get us some water!” he said, darting over to the sideboard.
59
+
60
+ He returned quickly with two glasses of water. Hermione took hers and thanked him, taking in a big gulp. As soon as she swallowed, Harry saw her eyes widen as her heart began beating wildly in her chest.
61
+
62
+ It worked! Harry thought, grinning to himself. Amorentia truly was the world’s best love potion, its effects lasting twice as long and being five times stronger.
63
+ """.strip(),
64
+ open=True,
65
+ ),
66
+ ],
67
+ )
68
+
69
+
70
+ def build_assistant_prompt() -> OpusV1Prompt:
71
+ return OpusV1Prompt(
72
+ turns=[
73
+ OpusV1Turn(
74
+ role="system",
75
+ content="You are an intelligent, knowledgeable, helpful, general-purpose assistant.",
76
+ ),
77
+ OpusV1Turn(
78
+ role="user",
79
+ content="Give me a sentence where every word begins with 'S'",
80
+ ),
81
+ ]
82
+ )
83
+
84
+
85
+ # %%
86
+
87
+
88
+ def main():
89
+ sampling_params = SamplingParams(
90
+ # I usually stay between 0.0 and 1.0, especially for the Yi models I found lower tends to be better.
91
+ # For assistant tasks, I usually use 0.0.
92
+ temperature=0.0,
93
+ min_p=0.05,
94
+ presence_penalty=0.1,
95
+ frequency_penalty=0.1,
96
+ repetition_penalty=1.1,
97
+ max_tokens=200,
98
+ ignore_eos=True,
99
+ skip_special_tokens=False,
100
+ spaces_between_special_tokens=False,
101
+ )
102
+
103
+ # Set max_model_len to fit in memory.
104
+ model = LLM(
105
+ "dreamgen/opus-v1.2-7b",
106
+ max_model_len=2000,
107
+ enforce_eager=True,
108
+ swap_space=0,
109
+ gpu_memory_utilization=0.85,
110
+ )
111
+
112
+ story_prompt = build_story_prompt()
113
+ print(format_opus_v1_prompt(story_prompt))
114
+
115
+ output = model.generate(format_opus_v1_prompt(story_prompt), sampling_params)
116
+ print(output[0].outputs[0].text)
117
+
118
+ # Expected:
119
+ """
120
+ It would make her fall deeply in love with him, and then he could use her to get what he wanted.
121
+
122
+ “Harry, what’s going on? You look so happy!” Hermione asked, smiling at him.
123
+
124
+ “Oh, well, I guess I am,” Harry replied, trying not to laugh. “I mean, I’ve always known that you were the one for me.”
125
+
126
+ “Really?” Hermione asked, blushing slightly. “I didn’t know that.”
127
+
128
+ “Yeah, I’ve always had feelings for you,” Harry said, leaning forward and placing his hand on top of hers. “And now that I’ve got you alone, I can finally tell you how much I care about you.”
129
+ """
130
+
131
+
132
+ main()