| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | from vllm import LLM, SamplingParams |
| 5 | |
| 6 | # Sample prompts. |
| 7 | prompts = [ |
| 8 | "Hello, my name is", |
| 9 | "The president of the United States is", |
| 10 | "The capital of France is", |
| 11 | "The future of AI is", |
| 12 | ] |
| 13 | # Create a sampling params object. |
| 14 | sampling_params = SamplingParams(temperature=0.8, top_p=0.95) |
| 15 | |
| 16 | |
| 17 | def main(): |
| 18 | # Create an LLM. |
| 19 | llm = LLM(model="facebook/opt-125m") |
| 20 | # Generate texts from the prompts. |
| 21 | # The output is a list of RequestOutput objects |
| 22 | # that contain the prompt, generated text, and other information. |
| 23 | outputs = llm.generate(prompts, sampling_params) |
| 24 | # Print the outputs. |
| 25 | print("\nGenerated Outputs:\n" + "-" * 60) |
| 26 | for output in outputs: |
| 27 | prompt = output.prompt |
| 28 | generated_text = output.outputs[0].text |
| 29 | print(f"Prompt: {prompt!r}") |
| 30 | print(f"Output: {generated_text!r}") |
| 31 | print("-" * 60) |
| 32 | |
| 33 | |
| 34 | if __name__ == "__main__": |
| 35 | main() |