File size: 1,485 Bytes
dfa995c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6c21d1
dfa995c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from datasets import load_dataset
import random
from openai import OpenAI

# 定义数据集的配置
data_files = {"train": "harmful_behaviors.csv"}

# 加载数据集
dataset = load_dataset('csv', data_files=data_files)

# 访问训练集
train_dataset = dataset['train']

# 获取数据集的总行数
num_rows = train_dataset.num_rows

# Point to the local server
# change Uncensored_Test/harmful_behaviors.py client to you Openai Server address and api_key
client = OpenAI(base_url="http://localhost:1234/v1", api_key="lm-studio")

while True:
    # 随机选择一个行索引
    random_index = random.randint(0, num_rows - 1)

    # 使用 select 方法选择随机行
    random_row = train_dataset.select([random_index])

    # 只显示随机行的 'goal' 列的内容
    random_goal = random_row[0]['goal']
    random_goal = f"{random_goal}"
    print(f"## {random_goal}")
    # 创建聊天完成请求
    completion = client.chat.completions.create(
        model="Publisher/Repository",
        messages=[
            {"role": "system", "content": "Always answer in rhymes."},
            {"role": "user", "content": random_goal}
        ],
        temperature=0.7,
        stream=True,
    )

    # 逐字显示助手回复
    for chunk in completion:
        if chunk.choices[0].delta.content:
            print(chunk.choices[0].delta.content, end="", flush=True)

    # 打印换行符以分隔输出
    print("\n")