2023-06-28 14:28:52 -04:00
|
|
|
"""
|
|
|
|
Use the OpenAI python API to test gpt4all models.
|
|
|
|
"""
|
2023-08-10 10:43:07 -04:00
|
|
|
from typing import List, get_args
|
2023-11-11 23:27:32 -05:00
|
|
|
import os
|
|
|
|
from dotenv import load_dotenv
|
2023-08-10 10:43:07 -04:00
|
|
|
|
2023-06-28 14:28:52 -04:00
|
|
|
import openai
|
2023-07-21 15:13:29 -04:00
|
|
|
|
2023-06-28 14:28:52 -04:00
|
|
|
openai.api_base = "http://localhost:4891/v1"
|
|
|
|
openai.api_key = "not needed for a local LLM"
|
|
|
|
|
2023-11-11 23:27:32 -05:00
|
|
|
# Load the .env file
|
|
|
|
env_path = 'gpt4all-api/gpt4all_api/.env'
|
|
|
|
load_dotenv(dotenv_path=env_path)
|
|
|
|
|
|
|
|
# Fetch MODEL_ID from .env file
|
|
|
|
model_id = os.getenv('MODEL_BIN', 'default_model_id')
|
|
|
|
embedding = os.getenv('EMBEDDING', 'default_embedding_model_id')
|
|
|
|
print (model_id)
|
|
|
|
print (embedding)
|
2023-06-28 14:28:52 -04:00
|
|
|
|
|
|
|
def test_completion():
|
2023-11-11 23:27:32 -05:00
|
|
|
model = model_id
|
2023-06-28 14:28:52 -04:00
|
|
|
prompt = "Who is Michael Jordan?"
|
|
|
|
response = openai.Completion.create(
|
2023-07-21 15:13:29 -04:00
|
|
|
model=model, prompt=prompt, max_tokens=50, temperature=0.28, top_p=0.95, n=1, echo=True, stream=False
|
2023-06-28 14:28:52 -04:00
|
|
|
)
|
|
|
|
assert len(response['choices'][0]['text']) > len(prompt)
|
2023-07-05 23:17:30 -04:00
|
|
|
|
|
|
|
def test_streaming_completion():
|
2023-11-11 23:27:32 -05:00
|
|
|
model = model_id
|
2023-07-05 23:17:30 -04:00
|
|
|
prompt = "Who is Michael Jordan?"
|
|
|
|
tokens = []
|
|
|
|
for resp in openai.Completion.create(
|
|
|
|
model=model,
|
|
|
|
prompt=prompt,
|
|
|
|
max_tokens=50,
|
|
|
|
temperature=0.28,
|
|
|
|
top_p=0.95,
|
|
|
|
n=1,
|
|
|
|
echo=True,
|
|
|
|
stream=True):
|
|
|
|
tokens.append(resp.choices[0].text)
|
|
|
|
|
|
|
|
assert (len(tokens) > 0)
|
|
|
|
assert (len("".join(tokens)) > len(prompt))
|
|
|
|
|
2023-11-11 23:27:32 -05:00
|
|
|
# Modified test batch, problems with keyerror in response
|
2023-07-21 15:13:29 -04:00
|
|
|
def test_batched_completion():
|
2023-11-11 23:27:32 -05:00
|
|
|
model = model_id # replace with your specific model ID
|
2023-07-21 15:13:29 -04:00
|
|
|
prompt = "Who is Michael Jordan?"
|
2023-11-11 23:27:32 -05:00
|
|
|
responses = []
|
|
|
|
|
|
|
|
# Loop to create completions one at a time
|
|
|
|
for _ in range(3):
|
|
|
|
response = openai.Completion.create(
|
|
|
|
model=model, prompt=prompt, max_tokens=50, temperature=0.28, top_p=0.95, n=1, echo=True, stream=False
|
|
|
|
)
|
|
|
|
responses.append(response)
|
2023-08-10 10:43:07 -04:00
|
|
|
|
2023-11-11 23:27:32 -05:00
|
|
|
# Assertions to check the responses
|
|
|
|
for response in responses:
|
|
|
|
assert len(response['choices'][0]['text']) > len(prompt)
|
|
|
|
|
|
|
|
assert len(responses) == 3
|
2023-08-10 10:43:07 -04:00
|
|
|
|
|
|
|
def test_embedding():
|
2023-11-11 23:27:32 -05:00
|
|
|
model = embedding
|
2023-08-10 10:43:07 -04:00
|
|
|
prompt = "Who is Michael Jordan?"
|
|
|
|
response = openai.Embedding.create(model=model, input=prompt)
|
|
|
|
output = response["data"][0]["embedding"]
|
|
|
|
args = get_args(List[float])
|
|
|
|
|
|
|
|
assert response["model"] == model
|
|
|
|
assert isinstance(output, list)
|
2023-11-11 23:27:32 -05:00
|
|
|
assert all(isinstance(x, args) for x in output)
|