OpenAI Chat Completion Client
Source vllm-project/vllm.
1from openai import OpenAI23# Modify OpenAI's API key and API base to use vLLM's API server.4openai_api_key = "EMPTY"5openai_api_base = "http://localhost:8000/v1"67client = OpenAI(8 # defaults to os.environ.get("OPENAI_API_KEY")9 api_key=openai_api_key,10 base_url=openai_api_base,11)1213models = client.models.list()14model = models.data[0].id1516chat_completion = client.chat.completions.create(17 messages=[{18 "role": "system",19 "content": "You are a helpful assistant."20 }, {21 "role": "user",22 "content": "Who won the world series in 2020?"23 }, {24 "role":25 "assistant",26 "content":27 "The Los Angeles Dodgers won the World Series in 2020."28 }, {29 "role": "user",30 "content": "Where was it played?"31 }],32 model=model,33)3435print("Chat completion results:")36print(chat_completion)