File size: 1,589 Bytes
fb25388
1717121
886091f
e17e77b
 
 
 
 
 
 
1717121
e17e77b
1717121
e17e77b
 
 
 
 
 
 
 
 
 
fb25388
 
 
 
e17e77b
 
1717121
 
fb25388
1717121
 
e17e77b
 
 
 
 
 
 
 
 
 
fb25388
e17e77b
fb25388
 
e17e77b
 
1717121
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
import os
import qianfan

from ..presets import *
from ..utils import *

from .base_model import BaseLLMModel


class ERNIE_Client(BaseLLMModel):
    def __init__(self, model_name, api_key, secret_key) -> None:
        super().__init__(model_name=model_name)
        self.chat_completion = qianfan.ChatCompletion(ak=api_key, sk=secret_key)

    def get_answer_stream_iter(self):
        system_prompt = self.system_prompt
        history = self.history
        if system_prompt is not None:
            history = [construct_system(system_prompt), *history]

        # 去除history中 history的role为system的
        history = [i for i in history if i["role"] != "system"]

        data = {
            "messages": history,
            "top_p": self.top_p,
            "temperature": self.temperature,
        }

        response = self.chat_completion.do(model=self.model_name, **data, stream=True)
        partial_text = ""
        for result in response:
            partial_text += result['result']
            yield partial_text

    def get_answer_at_once(self):
        system_prompt = self.system_prompt
        history = self.history
        if system_prompt is not None:
            history = [construct_system(system_prompt), *history]

        # 去除history中 history的role为system的
        history = [i for i in history if i["role"] != "system"]

        data = {
            "messages": history,
            "top_p": self.top_p,
            "temperature": self.temperature,
        }

        return self.chat_completion.do(model=self.model_name, **data)['result']