Gemini Backend (#9)

Co-authored-by: Ying Sheng <sqy1415@gmail.com>
This commit is contained in:
shiyi.c_98
2024-01-16 22:29:37 -08:00
committed by GitHub
parent c4707f1bb5
commit fd7c479239
13 changed files with 311 additions and 2 deletions

View 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())

View 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)

View 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)

Binary file not shown.

After

Width:  |  Height:  |  Size: 337 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 407 KiB