File size: 1,748 Bytes
9431844
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61

# -*- coding: utf-8 -*-
import requests
import streamlit as st
import openai
from openai import OpenAI
import os
from dotenv import load_dotenv
import numpy as np
import pandas as pd
import csv
import tempfile
from tempfile import NamedTemporaryFile
import pathlib
from pathlib import Path
import re
from re import sub
import matplotlib.pyplot as plt
from itertools import product
from tqdm import tqdm_notebook, tqdm, trange
import time
from time import sleep
import pretty_errors
import seaborn as sns
from matplotlib.pyplot import style
from rich import print
import warnings
warnings.filterwarnings('ignore')

load_dotenv()
### 设置openai的API key
os.environ["OPENAI_API_KEY"] = os.environ['user_token']
openai.api_key = os.environ['user_token']

##NOTE: 需要注意OpenAI的版本
def chatgpt(user_prompt, sys_prompt="You are professional consultant", openai_model="gpt-3.5-turbo-16k"):
    # print('chatgpt mode starts!')
    try:
        openai_client = OpenAI()
        chatgpt_response = openai_client.chat.completions.create(
            model=openai_model,
            messages=[
                {"role": "system", "content": sys_prompt},
                {"role": "user", "content": user_prompt},
            ],
            stream=False,
        )
        
        full_response = chatgpt_response.choices[0].message.content
        print(full_response)
    except Exception as e:
        print(e)
        full_response = "Sorry, I don't have an answer for that yet."
    
    # return chatgpt_response, full_response
    return full_response

# user_prompt = "where is Shanghai?"
# system_prompt = "You are a helpful AI assistant."
# full_response = chatgpt(user_prompt=user_prompt, sys_prompt=system_prompt)
# print(full_response)