#encoding=utf-8
import os
import sys
import openai
import tiktoken
import argparse
import re
import logging

class GPT():
    def __init__(self, model = "gpt-3.5-turbo", 
                 api_type="open_ai", api_base=None, api_key=None, api_version=None,
                 temperature = 0.0, system_prompt = "", post_prompt = "", use_history = False):
        self.model = model
        self.engine = model
        self.api_type = api_type
        self.api_base = api_base
        self.api_key = api_key
        self.api_version = api_version
        self.temperature = temperature
        self.system_prompt = system_prompt
        self.post_prompt = post_prompt
        self.use_history = use_history
        self.history = []
        #openai.api_key = os.getenv("OPENAI_API_KEY")
    
    def set_system_prompt(self, system_prompt):
        self.system_prompt = system_prompt
    
    def set_post_prompt(self, post_prompt):
        self.post_prompt = post_prompt
    
    def set_use_history(self, use_history):
        self.use_history = use_history

    def set_history(self, history):
        self.history = history

    # Returns the number of tokens in a text string
    # todo: not sure if gpt2 is ok for chat complition
    def num_tokens_from_string(self, string: str) -> int:
        encoding = tiktoken.get_encoding("gpt2")
        num_tokens = len(encoding.encode(string))
        return num_tokens

    # return how many token can be added to context and query
    # todo this function is not correct, need to debug
    def get_token_limit(self):
        if self.model == "gpt-3.5-turbo":
            # for gpt-3.5-turbo, the max_tokens is 4096
            max_tokens = 4096
        # todo: debug when using gpt-4
        elif self.model == "gpt-4":
            max_tokens = 8192
        else:
            max_tokens = 4096
        logging.debug("history:", self.history)
        #print("content:", str(text.get('content')) for text in self.history)
        for text in self.history:
            logging.debug(text.get('content'))
        history_token = sum([self.num_tokens_from_string(str(text)) for text in self.history])
        
        # 50 is for fixed system prompt and post prompt
        return max_tokens - history_token - 50

    # split text into paragraphs
    # first split text into paragraphs by '\n'
    # then split paragraphs into sentences by '.', '?', '!'
    # at last if the sentence is too long, split it by max_tokens
    # this code is mostly generated by GPT-4 and bugfixed by me
    def split_text(self, text, max_tokens=1800):
        paragraphs = text.split('\n')
        chunks = []
        current_chunk = ''
        current_tokens = 0

        def split_text_by_sentence(text, max_tokens):
            sentences = re.split(r'(?<=[.!?。！？])\s*', text)
            chunks = []
            current_chunk = ''
            current_tokens = 0

            for sentence in sentences:
                sentence_tokens = self.num_tokens_from_string(sentence)

                if current_tokens + sentence_tokens + 1 > max_tokens:
                    chunks.append(current_chunk)
                    current_chunk = sentence
                    current_tokens = sentence_tokens
                else:
                    if current_chunk:
                        current_chunk += ' '
                    #    current_tokens += 1
                    current_chunk += sentence
                    current_tokens = self.num_tokens_from_string(current_chunk)

            if current_chunk:
                chunks.append(current_chunk)

            return chunks

        for paragraph in paragraphs:
            paragraph_tokens = self.num_tokens_from_string(paragraph)

            if paragraph_tokens > max_tokens and current_chunk == '':
                chunks.extend(split_text_by_sentence(paragraph, max_tokens))
                continue

            if current_tokens + paragraph_tokens + 1 > max_tokens:
                current_chunk += '\n'
                chunks.append(current_chunk)
                #print(f"append chunk: {current_chunk} tokens: {current_tokens}")

                if paragraph_tokens > max_tokens:
                    chunks.extend(split_text_by_sentence(paragraph, max_tokens))
                    continue
                else:
                    current_chunk = paragraph
                    current_tokens = self.num_tokens_from_string(paragraph)
            else:
                if current_chunk:
                    current_chunk += '\n'
                current_chunk += paragraph
                current_tokens = self.num_tokens_from_string(current_chunk)
                #print(f"concat paragraph: {paragraph} tokens: {current_tokens}")

        if current_chunk:
            chunks.append(current_chunk)

        return chunks

    # context_info is a list of strings
    # query_text is a string
    def query(self, context_info, query_text):
        # todo: check query_message length if it is over 4096 the throw exception  
        if (context_info is None) or (context_info == ""):
            text = ""
        else:
            text = "Please reply based on the context information below:\n".join(f"{index}. {text}" for index, text in enumerate(context_info))
        if self.use_history:
            query_message = [
                {'role': 'system',
                'content': f'{self.system_prompt} {text}'},
                *self.history,
                {'role': 'user', 'content': query_text + self.post_prompt},
            ]
            logging.debug(query_message)
            response = openai.ChatCompletion.create(
                model=self.model,
                engine=self.engine,
                api_type=self.api_type, 
                api_key=self.api_key, 
                api_base=self.api_base, 
                api_version=self.api_version,
                temperature=self.temperature,
                messages=query_message
            )
            self.history.append({'role': 'user', 'content': query_text})
            self.history.append({'role': 'assistant', 'content': response.choices[0].message.content})
        else:
            query_message = [
                {'role': 'system',
                'content': f'{self.system_prompt} {text}'},
                {'role': 'user', 'content': query_text + self.post_prompt},
            ]
            logging.debug(query_message)
            response = openai.ChatCompletion.create(
                model=self.model,
                engine=self.engine,
                api_type=self.api_type, 
                api_key=self.api_key, 
                api_base=self.api_base, 
                api_version=self.api_version,
                temperature=self.temperature,
                messages=query_message
            )

        logging.debug(response)

        return response.choices[0].message.content

# test cases
def test_split_text(gpt):
    # 测试用例 1： 简单文本，无需拆分
    text1 = "This is a short text. It doesn't need to be split."
    result1 = gpt.split_text(text1)
    print(f"Test case 1 result: {result1}")
    print("len(result1):", len(result1))
    assert len(result1) == 1

    # 测试用例 2： 长文本，需按段落拆分
    text2 = "hello how are you. " * int(1800/5 - 1) + "\n" + "Hello How are you. " * int(1800/5 - 1)
    result2 = gpt.split_text(text2)
    print(f"Test case 2 result: {result2}")
    print("len(result2):", len(result2))
    assert len(result2) == 2

    # 测试用例 3： 长文本，需按句子拆分
    text3 = "hello there how are you." * 400
    result3 = gpt.split_text(text3)
    print(f"Test case 3 result: {result3}")  # 打印 result3 的内容
    print("len(result3):", len(result3))
    assert len(result3) == 2


    # 测试用例 4： 长文本，需按 token 数量拆分
    text4 = "hello there how are you." * int(1900 / 5)
    result4 = gpt.split_text(text4)
    print(f"Test case 4 result: {result4}")
    print("len(result4):", len(result4))
    assert len(result4) == 2

    # test case 5: 多个段落合并
    text5 = "hello there how are you.\n" * 10
    result5 = gpt.split_text(text5)
    print("len(result5):", len(result5))
    assert len(result5) == 1

    text6 = '''1. What is AI-Based Remaining Range?
Combining AI algorithm with the previous remaining mileage algorithm, AI-Based Remaining Range is calculated in accordance with user's riding habits, vehicle status and riding environment. We hope to provide users with more accurate remaining range along with the development of AI.
&nbsp;
2. What can this algorithm learn?
Although users use the same type of vehicle in different cities, the actual range may vary due to the factors of driving habits, road conditions and vehicle status. It is our hope that, with AI-Based Remaining Range Algorithm, the remaining range displays on the app or the dashboard will be more accurate and customized for users, which may further alleviate user's mileage anxiety and help users make travel plans.

1. Seasons

The weather changes in four seasons have great effects on the actual range.
As we all know, the battery level of electric products may drop quickly in winter, and the vehicle even powers off automatically. It&rsquo;s the same for eScooters with lead-acid batteries or lithium batteries. The reaction of chemical substances in the battery in a low temperature environment slows down, and the charging and discharging power will drop significantly, which will make charging time longer and the range may be not as durable as usual. When the weather gets warmer, the battery performance will get back to normal state, and the actual range will be normal.

2. Driving habits

For eScooters, there is a concept called Economical Speed, which means that the power consumption is minimum when you drive the vehicle at a certain constant speed. Bad driving habits such as suddenly driving at full throttle or hard braking will do bring more joy for your drive indeed, but the range will also be decreased. When we compare the physical exertio of walking with sprinting, the physical exertion caused by sprinting will lead to a greater decrease in endurance.
In addition, the other factor is load. The range may decrease by a certain degree if the vehicle is driven by two people or carrying heavy things.

3. Road condition

For the AI algorithm updated this time, we took road condition into account. Besides of road condition, we also considered other factors related to road condition, tire pressure, wind resistance, etc.
Road condition: Flat roads are the most power-saving, the range will be decreased greatly when driving in winding and bumpy gravel roads or most uphill road conditions.
Tire pressure: Under-inflated tires will cause greater driving resistance. Compared with tires with standard tire pressure, the range can be decreased by up to 30%.
Wind resistance: Normally, people wear more clothes in winter, compared with summer, the wind resistance is greater. The battery consumption is more obvious when you drive at a high speed.

4. Future

The current algorithm model vaguely contains the factors including vehicle health, tire pressure, motor efficiency, etc. In the future, we will continue the research in this respect as well as the R&amp;D related to vehicle maintenance that based on big data, so as to improve the range and maintain the vehicle condition by enabling users to fix their bad driving habits.
Notes:
Winter is the season with the most complex, obvious and challenging impact on the vehicle range endurance algorithm. That&rsquo;s why we choose test in winter, we hope to overcome the biggest challenges and variables in this season and provide users with more accurate mileage data.
After tracking riding records for about two weeks or collecting enough riding mileages data, more accurate remaining mileage may be displayed. If the remaining mileage is not accurate, please record more rides or ride more mileages.
If the actual remaining range deviates from the displayed range every time after long-time riding, please report to customer service, and our technicians will check your vehicle condition.
'''
    result6 = gpt.split_text(text5)
    print("len(result6):", len(result6))
    assert len(result6) == 1

    print("All test cases passed.")

import os
import openai
openai.api_type = "azure"
openai.api_base = "https://ninebot-rd-openai-1.openai.azure.com/"
openai.api_version = "2022-12-01"
openai.api_key = os.getenv("OPENAI_API_KEY")

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="GPT")
    parser.add_argument("--model", default="gpt-3.5-turbo", dest="model", type=str,help="GPT model")
    parser.add_argument("--temperature", default=0.0, dest="temperature", type=float,help="GPT temperature")
    parser.add_argument("--system_prompt", default="You are a customer service agent for company 9. You need to reply to the customer based on the context information below.", dest="system_prompt", type=str,help="GPT system prompt")
    parser.add_argument("--post_prompt", default="Please reply to the customer.", dest="post_prompt", type=str,help="GPT post prompt")
    parser.add_argument("--use_history", default=False, dest="use_history", type=bool,help="GPT use history")
    parser.add_argument("--history", default=[], dest="history", type=list,help="GPT history")
    args = parser.parse_args()
    
    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    if False:
        #test for openai
        gpt = GPT(model = args.model, temperature = args.temperature, system_prompt = args.system_prompt, post_prompt = args.post_prompt, use_history = args.use_history)
    else:
        # test for azure
        gpt = GPT(
            model="model-gpt-35-turbo-0301", 
            api_type="azure", 
            api_base = "https://ninebot-rd-openai-1.openai.azure.com/",
            api_version = "2022-12-01",
            temperature = args.temperature, 
            system_prompt = args.system_prompt, 
            post_prompt = args.post_prompt, 
            use_history = args.use_history)

    # tests

    # test 1 split_text
    #test_split_text(gpt)
    #exit()

    gpt.set_system_prompt("You are a customer service agent for Segway. You need to reply to the customer based on the context information below:")
    gpt.set_post_prompt("Reply '我不清楚您的问题' on unrelated to context.")

    #gpt.set_history([{'role':'user', 'content':'asdfawefasdfasdf我不清楚您的问题。asdfawef我不清楚您的问题。我想要买车很抱歉，Segway不是汽车，它是一种电动平衡车。如果您对Segway感兴趣，可以前往长安街上的Segway店铺购买。真的吗我不清楚您的问题。那好吧,我应该去哪里买segway您可以前往长安街上的Segway店铺购买Segway。他们会为您提供更多的信息和帮助。it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beauti我不清楚您的问题。请提供相关的问题或信息，我会尽力回答您的疑问。it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beauti很抱歉，我之前的回复与您的信息无关。如果您有任何问题或需要帮助，请告诉我，我会尽力回答您的疑问。it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. 我不清楚您的问题，因为您的信息似乎与任何具体问题或主题无关。如果您有任何问题或需要帮助，请告诉我，我会尽力回答您的疑问。it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. 我不清楚您的问题，因为您的信息似乎与任何具体问题或主题无关。请提供相关的问题或信息，我会尽力回答您的疑问。it’s a beautiful day.我不清楚您的问题。it’s a beautiful day.我不清楚您的问题，因为您的信息似乎与任何具体问题或主题无关。it’s a beautiful day.我不清楚您的问题，因为您的信息似乎与之前的上下文无关。请提供相关的问题或信息，我会尽力回答您的疑问。it’s a beautiful day.我不清楚您的问题，因为您的信息似乎与之前的上下文无关。请提供相关的问题或信息，我会尽力回答您的疑问。it’s a beautiful day.我不清楚您的问题，因为您的信息似乎与之前的上下文无关。请提供相关的问题或信息，我会尽力回答您的疑问。it’s a beautiful day.我不清楚您的问题，因为您的信息似乎与之前的上下文无关。请提供相关的问题或信息，我会尽力回答您的疑问。it’s a beautiful day.我不清楚您的问题，因为您的信息似乎与之前的上下文无关。请提供相关的问题或信息，我会尽力回答您的疑问。it’s a beautiful day.我不清楚您的问题，因为您的信息似乎与之前的上下文无关。请提供相关的问题或信息，我会尽力回答您的疑问。it’s a beautiful day.我不清楚您的问题。it’s a beautiful day.我不清楚您的问题，因为您的信息似乎与之前的上下文无关。请提供相关的问题或信息，我会尽力回答您的疑问。it’s a beautiful day.我不清楚您的问题，因为您的信息似乎与之前的上下文无关。请提供相关的问题或信息，我会尽力回答您的疑问。it’s a beautiful day.我不清楚您的问题。it’s a beautiful day.我不清楚您的问题。it’s a beautiful day.我不清楚您的问题，因为您的信息似乎与之前的上下文无关。请提供相关的问题或信息，我会尽力回答您的疑问。it’s a beautiful day.我不清楚您的问题，因为您的信息似乎与之前的上下文无关。请提供相关的问题或信息，我会尽力回答您的疑问。it’s a beautiful day.我不清楚您的问题，因为您的信息似乎与之前的上下文无关。请提供相关的问题或信息，我会尽力回答您的疑问。it’s a beautifulday.我不清楚您的问题，因为您的信息似乎与之前的上下文无关。请提供相关的问题或信息，我会尽力回答您的疑问。it’s a beautiful day.我不清楚您的问题。it’s a beautiful day.it’s a beautiful day.我不清楚您的问题。it’s a beautiful day.it’s a beautiful day.it’s a beautiful day.it’s a beautiful day.it’s a beautiful day.我不清楚您的问题。it’s a beautiful day.it’s a beautiful day.it’s a beautiful day.it’s a beautiful day.it’s a beautiful day.it’s a beautiful day.it’s a beautiful day.it’s a beautiful day.it’s a beautiful day.it’s a beautiful day.it’s a beautiful day.it’s a beautiful day.我不清楚您的问题。it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. it’s a beautiful day. 我不清'}])

    response = openai.Completion.create(
        engine="model-gpt-35-turbo-0301",
        prompt="Write a product launch email for new AI-powered headphones that are priced at $79.99 and available at Best Buy, Target and Amazon.com. The target audience is tech-savvy music lovers and the tone is friendly and exciting.\n\n1. What should be the subject line of the email?  \n2. What should be the body of the email?",
        temperature=1,
        max_tokens=350,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0,
        stop=None)

    print(response)


    print ("dp",openai.Completion.create(
                #model="model-gpt-35-turbo-0301", 
                engine="model-gpt-35-turbo-0301", 
                api_type="azure", 
                api_base = "https://ninebot-rd-openai-1.openai.azure.com/",
                api_version = "2022-12-01",
                temperature=0.0,
                prompt='hello'
            ))


    while True:
        query = input("请输入问题：")
        if query == "exit":
            break
        print(gpt.query(["Segway在长安街上有一家店，你可以去那里买Segway。"], query))
        print(gpt.get_token_limit())
    
