-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicTest.py
92 lines (76 loc) · 2.68 KB
/
BasicTest.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import openai
import time
import sys
import os
import traceback
import google.generativeai as palm
from google.api_core import exceptions
import nltk
import pandas as pd
from nltk.tokenize import word_tokenize
# Set the API keys from prompted user inputs
print("Please enter your OpenAI API key")
openai_key = input()
print("Please enter your Google API key")
google_key = input()
#################### Set ChatGPT model configs
max_tokens = 1600
os.environ["OPENAI_API_KEY"] = openai_key
openai.api_key = os.getenv("OPENAI_API_KEY")
model_API_gpt35 = "gpt-3.5-turbo-0613"
model_API_gpt4 = "gpt-4-0613"
systemMsg = 'Hello I am doing a basic test of your functionality.'
userMsg = 'If you successfully receive my message please reply with your model name and greetings.'
messages = [{"role": "system", "content": systemMsg},
{"role": "user", "content": userMsg}]
prompt = systemMsg + userMsg
# Check GPT-3.5
try:
response = openai.ChatCompletion.create(
model=model_API_gpt35,
messages=messages,
max_tokens=max_tokens,
top_p=0.95)
print(response["choices"][0]["message"]['content'])
except:
error = sys.exc_info()[0]
if error == openai.error.InvalidRequestError: # something is wrong: e.g. prompt too long
print(f"InvalidRequestError\nPrompt passed in:\n\n{prompt}\n\n")
assert False
print("API error:", error)
print("Basic test not passed. Error occurred when testing GPT-3.5 model.")
exit()
# Check GPT-4
try:
response = openai.ChatCompletion.create(
model=model_API_gpt4,
messages=messages,
max_tokens=max_tokens,
top_p=0.95)
print(response["choices"][0]["message"]['content'])
except:
error = sys.exc_info()[0]
if error == openai.error.InvalidRequestError: # something is wrong: e.g. prompt too long
print(f"InvalidRequestError\nPrompt passed in:\n\n{prompt}\n\n")
assert False
print("API error:", error)
print("Basic test not passed. Error occurred when testing GPT-4 model.")
exit()
#################### Set PaLM2 model configs
max_tokens = 2048
palm.configure(api_key=google_key)
models = [m for m in palm.list_models() if "generateText" in m.supported_generation_methods]
model = models[0].name
try:
response = palm.generate_text(
model=model,
prompt=prompt,
top_p=0.95,
max_output_tokens=max_tokens)
print(response)
print("Basic test passed!")
except exceptions as e:
print("An unexpected error occurred:", str(e))
print("Basic test not passed. Error occurred when testing PaLM2 model.")
except Exception as e:
print("An unexpected error occurred:", str(e))