|
| 1 | +from langtrace_python_sdk import langtrace |
| 2 | +import google.generativeai as genai |
| 3 | +from dotenv import load_dotenv |
| 4 | +import os |
| 5 | +import asyncio |
| 6 | +import pathlib |
| 7 | +from .function_tools import tools |
| 8 | + |
| 9 | +load_dotenv() |
| 10 | + |
| 11 | +langtrace.init(write_spans_to_console=False, batch=False) |
| 12 | +genai.configure(api_key=os.environ["GEMINI_API_KEY"]) |
| 13 | + |
| 14 | + |
| 15 | +async def async_demo(): |
| 16 | + task1 = asyncio.create_task(async_generate()) |
| 17 | + task2 = asyncio.create_task(async_generate(stream=True)) |
| 18 | + return await asyncio.gather(task1, task2) |
| 19 | + |
| 20 | + |
| 21 | +def basic(): |
| 22 | + generate() |
| 23 | + generate(stream=True, with_tools=True) |
| 24 | + |
| 25 | + # image_to_text() |
| 26 | + # audio_to_text() |
| 27 | + asyncio.run(async_demo()) |
| 28 | + |
| 29 | + |
| 30 | +def generate(stream=False, with_tools=False): |
| 31 | + model = genai.GenerativeModel( |
| 32 | + "gemini-1.5-pro", system_instruction="You are a cat. Your name is Neko." |
| 33 | + ) |
| 34 | + |
| 35 | + response = model.generate_content( |
| 36 | + "Write a story about a AI and magic", |
| 37 | + stream=stream, |
| 38 | + tools=tools if with_tools else None, |
| 39 | + ) |
| 40 | + if stream: |
| 41 | + for res in response: |
| 42 | + if res.text: |
| 43 | + print(res.text) |
| 44 | + else: |
| 45 | + print(response.text) |
| 46 | + |
| 47 | + |
| 48 | +async def async_generate(stream=False): |
| 49 | + model = genai.GenerativeModel( |
| 50 | + "gemini-1.5-pro", system_instruction="You are a cat. Your name is Neko." |
| 51 | + ) |
| 52 | + response = await model.generate_content_async( |
| 53 | + "Write a story about a AI and magic", stream=stream |
| 54 | + ) |
| 55 | + if stream: |
| 56 | + async for chunk in response: |
| 57 | + if chunk.text: |
| 58 | + print(chunk.text) |
| 59 | + else: |
| 60 | + print(response.text) |
| 61 | + |
| 62 | + |
| 63 | +def image_to_text(stream=False): |
| 64 | + model = genai.GenerativeModel("gemini-1.5-flash") |
| 65 | + image1 = { |
| 66 | + "mime_type": "image/jpeg", |
| 67 | + "data": pathlib.Path("src/examples/gemini_example/jetpack.jpg").read_bytes(), |
| 68 | + } |
| 69 | + |
| 70 | + prompt = "Describe me this picture. What do you see in it." |
| 71 | + response = model.generate_content([prompt, image1], stream=stream) |
| 72 | + if stream: |
| 73 | + for res in response: |
| 74 | + print(res.text) |
| 75 | + else: |
| 76 | + print(response.text) |
| 77 | + |
| 78 | + |
| 79 | +# def audio_to_text(stream=False): |
| 80 | +# model = genai.GenerativeModel("gemini-1.5-flash") |
| 81 | +# audio = genai.upload_file( |
| 82 | +# pathlib.Path("src/examples/gemini_example/voice_note.mp3") |
| 83 | +# ) |
| 84 | + |
| 85 | +# prompt = "Summarize this voice recording." |
| 86 | +# response = model.generate_content([prompt, audio], stream=stream) |
| 87 | +# if stream: |
| 88 | +# for res in response: |
| 89 | +# print(res.text) |
| 90 | +# else: |
| 91 | +# print(response.text) |
0 commit comments