-
Notifications
You must be signed in to change notification settings - Fork 0
/
datascope.py
42 lines (37 loc) · 1.83 KB
/
datascope.py
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
import random
from http import HTTPStatus
import dashscope
from dashscope import Generation
import yaml
config = yaml.safe_load(open("dojo.yaml"))
class PokkoaDataScope:
def __init__(self, model="qwen-turbo"):
self.model = model
self.model_namespace = "pokkoa.dojo.datascope"
self.system_rule = {"role": "system",
"content": "你是一个只解释易经卦像的bot,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。"}
dashscope.api_key = config["datascope"]["apikey"]
def completion(self, prompt, temperature=0.3, top_p=0.8, presence_penalty=0, frequency_penalty=1.1):
if frequency_penalty <= 0.0:
print(" Repetition_penalty should be greater than 0.0, using default")
frequency_penalty = 1.1
print(self.model_namespace, " with model", self.model)
messages = [self.system_rule,
{'role': 'user', 'content': prompt}]
response = Generation.call(
model=self.model,
messages=messages,
# 设置随机数种子seed,如果没有设置,则随机数种子默认为1234
seed=random.randint(1, 10000),
temperature=temperature,
top_p=top_p,
repetition_penalty=frequency_penalty,
# 将输出设置为"message"格式
result_format='message')
if response.status_code == HTTPStatus.OK:
return response.output.choices[0].message.content
else:
return ('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
response.request_id, response.status_code,
response.code, response.message
))