Migrate from OpenAI
Predibase supports OpenAI Chat Completions v1 compatible endpoints that makes it as easy as possible to migrate from OpenAI to Predibase.
Steps
- Obtain your Predibase API token
- For the
base_url
, obtain your tenant ID (found on the Settings > My Profile page) and deployment name (found on the Deployments page) - If using a fine-tuned adapter trained on Predibase, you'll need the adapter ID ("adapter repo name"/"version number"), which is included as the
model
parameter when prompting.
Python SDK Example
If you fine-tuned an adapter with apply_chat_template
, see the "Chat Completions" tab.
- Base Model
- Fine-tuned Adapter
- Chat Completions
from openai import OpenAI
api_token = "<PREDIBASE API TOKEN>"
tenant_id = "<PREDIBASE TENANT ID>"
model_name = "<DEPLOYMENT NAME>" # Ex. "mistral-7b"
base_url = f"https://serving.app.predibase.com/{tenant_id}/deployments/v2/llms/{model_name}/v1"
client = OpenAI(
api_key=api_token,
base_url=base_url,
)
completion = client.completions.create(
model="",
prompt="How many helicopters can a human eat in one sitting?",
max_tokens=100,
)
print("Completion result:", completion.choices[0].text)
from openai import OpenAI
api_token = "<PREDIBASE API TOKEN>"
tenant_id = "<PREDIBASE TENANT ID>"
model_name = "<DEPLOYMENT NAME>" # Ex. "mistral-7b"
adapter = "<ADAPTER REPO NAME>/<VERSION NUMBER>" # Ex. "adapter-repo/1"
base_url = f"https://serving.app.predibase.com/{tenant_id}/deployments/v2/llms/{model_name}/v1"
client = OpenAI(
api_key=api_token,
base_url=base_url,
)
completion = client.completions.create(
model=adapter,
prompt="How many helicopters can a human eat in one sitting?",
max_tokens=100,
)
print("Completion result:", completion.choices[0].text)
OpenAI Chat Completions automatically apply the chat template to your requests.
from openai import OpenAI
api_token = "<PREDIBASE API TOKEN>"
tenant_id = "<PREDIBASE TENANT ID>"
model_name = "<DEPLOYMENT NAME>" # Ex. "mistral-7b"
adapter = "<ADAPTER REPO NAME>/<VERSION NUMBER>" # Ex. "adapter-repo/1"
base_url = f"https://serving.app.predibase.com/{tenant_id}/deployments/v2/llms/{model_name}/v1"
client = OpenAI(
api_key=api_token,
base_url=base_url,
)
content = "How many helicopters can a human eat in one sitting?"
completion = client.chat.completions.create(
model=adapter,
messages=[{"role": "user", "content": content}],
max_tokens=100,
)
print("Completion result:", completion.choices[0].text)
Streaming
- Base Model
- Fine-tuned Adapter
from openai import OpenAI
api_token = "<PREDIBASE API TOKEN>"
tenant_id = "<PREDIBASE TENANT ID>"
model_name = "<DEPLOYMENT NAME>" # Ex. "mistral-7b"
base_url = f"https://serving.app.predibase.com/{tenant_id}/deployments/v2/llms/{model_name}/v1"
client = OpenAI(
api_key=api_token,
base_url=base_url,
)
# Prompt the base model
completion_stream = client.completions.create(
model="",
prompt="How many helicopters can a human eat in one sitting?",
max_tokens=100,
stream=True
)
text = []
for message in completion_stream:
print(message)
text.append(message.choices[0].text)
print("".join(text))
from openai import OpenAI
api_token = "<PREDIBASE API TOKEN>"
tenant_id = "<PREDIBASE TENANT ID>"
model_name = "<DEPLOYMENT NAME>" # Ex. "mistral-7b"
adapter = "<ADAPTER REPO NAME>/<VERSION NUMBER>" # Ex. "adapter-repo/1"
base_url = f"https://serving.app.predibase.com/{tenant_id}/deployments/v2/llms/{model_name}/v1"
client = OpenAI(
api_key=api_token,
base_url=base_url,
)
# Prompt the base model
completion_stream = client.completions.create(
model=adapter,
prompt="How many helicopters can a human eat in one sitting?",
max_tokens=100,
stream=True
)
response = []
for message in completion_stream:
token = message.choices[0].text
response.append(token)
print(token, end='')
print("".join(response))
REST API Example
export PREDIBASE_API_TOKEN="<YOUR TOKEN HERE>"
export PREDIBASE_ENDPOINT="<YOUR ENDPOINT HERE>"
#PREDIBASE_ENDPOINT_EXAMPLE: https://serving.app.predibase.com/{tenantID}/deployments/v2/llms/mistral-7b-instruct
curl -i $PREDIBASE_ENDPOINT/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${PREDIBASE_API_TOKEN}" \
-d '{"model": "", "messages": [{ "role": "user", "content": "How many helicopters can a human eat in one sitting?"}],"max_tokens": 100}'