diff --git a/1.py b/1.py new file mode 100644 index 000000000..86c09d627 --- /dev/null +++ b/1.py @@ -0,0 +1,35 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the terms described in the LICENSE file in +# the root directory of this source tree. + +import os + +from llama_stack_client import LlamaStackClient + +client = LlamaStackClient(base_url=f"http://localhost:{os.environ['LLAMA_STACK_PORT']}") + +response = client.inference.chat_completion( + model_id=os.environ["INFERENCE_MODEL"], + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Write a haiku about coding"}, + ], + response_format={ + "type": "json_schema", + "json_schema": { + "type": "object", + "properties": { + "completion_message": { + "type": "object", + "properties": { + "content": {"type": "string"}, + "additional_info": {"type": "string"}, + }, + } + }, + }, + }, +) +print(response.completion_message.content) diff --git a/2.py b/2.py new file mode 100644 index 000000000..989d93a65 --- /dev/null +++ b/2.py @@ -0,0 +1,39 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the terms described in the LICENSE file in +# the root directory of this source tree. + +from openai import OpenAI + +client = OpenAI(base_url="http://localhost:8000/v1", api_key="fake") + +completion = client.chat.completions.create( + model="meta-llama/Llama-3.2-3B-Instruct", + messages=[ + { + "role": "user", + "content": "Write me a haiku about coding", + }, + ], + extra_body={ + "response_format": { + "type": "json_schema", + "json_schema": { + "name": "name_of_response_format", + "schema": { + "type": "object", + "properties": { + "completion_message": { + "type": "string", + } + }, + "required": ["completion_message"], + }, + "strict": True, + }, + } + }, +) + +print(completion.choices[0].message.content)