Cloudera AI Inference service using OpenAI Python SDK client on a local machine
Consider the following guidelines for Cloudera AI Inference service using OpenAI Python SDK client.
Specify the
CDP_TOKEN
as the API_KEY. A common way
is to export CDP_TOKEN
as an environment variable and access that from
your Python code:
from openai import OpenAI
import os
API_KEY = os.environ['CDP_TOKEN']
MODEL_ID = [***MODEL_ID***]
client = OpenAI(
base_url = "[***BASE_URL***]",
api_key = API_KEY,
)
completion = client.chat.completions.create(
model=MODEL_ID,
messages=[{"role":"user","content":"Write a one-sentence definition of GenAI."}],
temperature=0.2,
top_p=0.7,
max_tokens=1024,
stream=True
)
for chunk in completion:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="")