26
examples/quick_start/gemini_example_complete.py
Normal file
26
examples/quick_start/gemini_example_complete.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from sglang import function, gen, set_default_backend, Gemini
|
||||
|
||||
|
||||
@function
|
||||
def few_shot_qa(s, question):
|
||||
s += (
|
||||
"""The following are questions with answers.
|
||||
Q: What is the capital of France?
|
||||
A: Paris
|
||||
Q: What is the capital of Germany?
|
||||
A: Berlin
|
||||
Q: What is the capital of Italy?
|
||||
A: Rome
|
||||
""")
|
||||
s += "Q: " + question + "\n"
|
||||
s += "A:" + gen("answer", stop="\n", temperature=0)
|
||||
|
||||
|
||||
set_default_backend(Gemini("gemini-pro"))
|
||||
|
||||
state = few_shot_qa.run(question="What is the capital of the United States?")
|
||||
answer = state["answer"].strip().lower()
|
||||
|
||||
assert "washington" in answer, f"answer: {state['answer']}"
|
||||
|
||||
print(state.text())
|
||||
19
examples/quick_start/gemini_example_multimodal_chat.py
Normal file
19
examples/quick_start/gemini_example_multimodal_chat.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from sglang import function, user, assistant, gen, image, set_default_backend, Gemini
|
||||
|
||||
|
||||
@function
|
||||
def image_qa(s, image_file1, image_file2, question):
|
||||
s += user(image(image_file1) + image(image_file2) + question)
|
||||
s += assistant(gen("answer_1", max_tokens=256))
|
||||
|
||||
set_default_backend(Gemini("gemini-pro-vision"))
|
||||
|
||||
state = image_qa.run(
|
||||
image_file1="./images/cat.jpeg",
|
||||
image_file2="./images/dog.jpeg",
|
||||
question="Describe difference of the 2 images in one sentence.",
|
||||
stream=True
|
||||
)
|
||||
|
||||
for out in state.text_iter():
|
||||
print(out, end="", flush=True)
|
||||
20
examples/quick_start/gemini_example_stream.py
Normal file
20
examples/quick_start/gemini_example_stream.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from sglang import function, user, assistant, gen, set_default_backend, Gemini
|
||||
|
||||
|
||||
@function
|
||||
def multi_turn_question(s, question_1, question_2):
|
||||
s += user(question_1)
|
||||
s += assistant(gen("answer_1", max_tokens=256))
|
||||
s += user(question_2)
|
||||
s += assistant(gen("answer_2", max_tokens=256))
|
||||
|
||||
set_default_backend(Gemini("gemini-pro"))
|
||||
|
||||
state = multi_turn_question.run(
|
||||
question_1="What is the capital of the United States?",
|
||||
question_2="List two local attractions.",
|
||||
stream=True
|
||||
)
|
||||
|
||||
for out in state.text_iter():
|
||||
print(out, end="", flush=True)
|
||||
BIN
examples/quick_start/images/cat.jpeg
Normal file
BIN
examples/quick_start/images/cat.jpeg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 337 KiB |
BIN
examples/quick_start/images/dog.jpeg
Normal file
BIN
examples/quick_start/images/dog.jpeg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 407 KiB |
Reference in New Issue
Block a user