Skip to content

Commit d2fb064

Browse files
committed
Adding util to help developers add message attachment
Summary: Adding `MessageAttachment.base64(path)` so that sending attachment will look like this: ``` from llama_stack_client.lib.inference.utils import MessageAttachment response = client.inference.chat_completion( model_id="meta-llama/llama3.2-11b-vision-instruct", messages=[ { "role": "user", "content": { "type": "image", "image": { "data": MessageAttachment.base64("images/tennis-game.png") } } }, { "role": "user", "content": "What's in this image?", } ] ) ``` Test Plan: ``` pip install . # start a new notebook and run from llama_stack_client import LlamaStackClient from llama_stack_client.lib.inference.utils import MessageAttachment client = LlamaStackClient( base_url='localhost:8321' ) response = client.inference.chat_completion( model_id="meta-llama/llama3.2-11b-vision-instruct", messages=[ { "role": "user", "content": { "type": "image", "image": { "data": MessageAttachment.base64("images/tennis-game.png") } } }, { "role": "user", "content": "What's in this image?", } ] ) print(response) ```
1 parent b183fb6 commit d2fb064

File tree

1 file changed

+20
-0
lines changed
  • src/llama_stack_client/lib/inference

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the terms described in the LICENSE file in
5+
# the root directory of this source tree.
6+
7+
import pathlib, base64
8+
9+
10+
class MessageAttachment:
11+
# https://developer.mozilla.org/en-US/docs/Glossary/Base64
12+
@classmethod
13+
def base64(cls, file_path: str) -> str:
14+
path = pathlib.Path(file_path)
15+
return base64.b64encode(path.read_bytes()).decode("utf-8")
16+
17+
# https://developer.mozilla.org/en-US/docs/Web/URI/Schemes/data
18+
@classmethod
19+
def data_url(cls, media_type: str, file_path: str) -> str:
20+
return f"data:{media_type};base64,{cls.base64(file_path)}"

0 commit comments

Comments
 (0)