gemini_example_multimodal_chat.py 738 Bytes
Newer Older
1
2
3
4
5
"""
Usage:
export GCP_PROJECT_ID=******
python3 gemini_example_multimodal_chat.py
"""
zhyncs's avatar
zhyncs committed
6

7
import sglang as sgl
shiyi.c_98's avatar
shiyi.c_98 committed
8
9


10
@sgl.function
shiyi.c_98's avatar
shiyi.c_98 committed
11
def image_qa(s, image_file1, image_file2, question):
12
13
    s += sgl.user(sgl.image(image_file1) + sgl.image(image_file2) + question)
    s += sgl.assistant(sgl.gen("answer", max_tokens=256))
shiyi.c_98's avatar
shiyi.c_98 committed
14
15


16
17
if __name__ == "__main__":
    sgl.set_default_backend(sgl.VertexAI("gemini-pro-vision"))
shiyi.c_98's avatar
shiyi.c_98 committed
18

19
20
21
22
    state = image_qa.run(
        image_file1="./images/cat.jpeg",
        image_file2="./images/dog.jpeg",
        question="Describe difference of the two images in one sentence.",
zhyncs's avatar
zhyncs committed
23
        stream=True,
24
25
26
27
28
29
30
    )

    for out in state.text_iter("answer"):
        print(out, end="", flush=True)
    print()

    print(state["answer"])