|
| 1 | +import vertexai |
| 2 | +import base64 |
| 3 | +import asyncio |
| 4 | +import vertexai.preview.generative_models as generative_models |
| 5 | +from vertexai.language_models import ChatModel, InputOutputTextPair, TextGenerationModel |
| 6 | +from langtrace_python_sdk import langtrace |
| 7 | +from vertexai.generative_models import GenerativeModel, Part, FinishReason |
| 8 | +from dotenv import load_dotenv |
| 9 | + |
| 10 | +load_dotenv() |
| 11 | + |
| 12 | +langtrace.init(write_spans_to_console=True, batch=False) |
| 13 | +vertexai.init(project="model-palace-429011-f5", location="us-central1") |
| 14 | + |
| 15 | + |
| 16 | +def basic(): |
| 17 | + # chat() |
| 18 | + # chat_streaming() |
| 19 | + # streaming_prediction() |
| 20 | + # asyncio.run(async_streaming_prediction()) |
| 21 | + |
| 22 | + generate() |
| 23 | + generate(stream=True) |
| 24 | + |
| 25 | + image_to_text() |
| 26 | + image_to_text(stream=True) |
| 27 | + |
| 28 | + video_to_text() |
| 29 | + video_to_text(stream=True) |
| 30 | + |
| 31 | + audio_to_text() |
| 32 | + audio_to_text(stream=True) |
| 33 | + |
| 34 | + |
| 35 | +def chat(): |
| 36 | + """Chat Example with a Large Language Model""" |
| 37 | + |
| 38 | + chat_model = ChatModel.from_pretrained("chat-bison") |
| 39 | + |
| 40 | + parameters = { |
| 41 | + "temperature": 0.8, |
| 42 | + "max_output_tokens": 256, |
| 43 | + "top_p": 0.95, |
| 44 | + "top_k": 40, |
| 45 | + } |
| 46 | + |
| 47 | + chat = chat_model.start_chat( |
| 48 | + context="My name is Miles. You are an astronomer, knowledgeable about the solar system.", |
| 49 | + examples=[ |
| 50 | + InputOutputTextPair( |
| 51 | + input_text="How many moons does Mars have?", |
| 52 | + output_text="The planet Mars has two moons, Phobos and Deimos.", |
| 53 | + ), |
| 54 | + ], |
| 55 | + ) |
| 56 | + |
| 57 | + response = chat.send_message( |
| 58 | + message="How many planets are there in the solar system?", **parameters |
| 59 | + ) |
| 60 | + |
| 61 | + return response |
| 62 | + |
| 63 | + |
| 64 | +def chat_streaming() -> str: |
| 65 | + """Streaming Chat Example with a Large Language Model""" |
| 66 | + |
| 67 | + chat_model = ChatModel.from_pretrained("chat-bison") |
| 68 | + |
| 69 | + parameters = { |
| 70 | + "temperature": 0.8, |
| 71 | + "max_output_tokens": 256, |
| 72 | + "top_p": 0.95, |
| 73 | + "top_k": 40, |
| 74 | + } |
| 75 | + |
| 76 | + chat = chat_model.start_chat( |
| 77 | + context="My name is Miles. You are an astronomer, knowledgeable about the solar system.", |
| 78 | + examples=[ |
| 79 | + InputOutputTextPair( |
| 80 | + input_text="How many moons does Mars have?", |
| 81 | + output_text="The planet Mars has two moons, Phobos and Deimos.", |
| 82 | + ), |
| 83 | + ], |
| 84 | + ) |
| 85 | + |
| 86 | + responses = chat.send_message_streaming( |
| 87 | + message="How many planets are there in the solar system?", **parameters |
| 88 | + ) |
| 89 | + |
| 90 | + result = [response for response in responses] |
| 91 | + return result |
| 92 | + |
| 93 | + |
| 94 | +def streaming_prediction() -> str: |
| 95 | + """Streaming Text Example with a Large Language Model""" |
| 96 | + |
| 97 | + text_generation_model = TextGenerationModel.from_pretrained("text-bison") |
| 98 | + parameters = { |
| 99 | + "max_output_tokens": 256, |
| 100 | + "top_p": 0.8, |
| 101 | + "top_k": 40, |
| 102 | + } |
| 103 | + responses = text_generation_model.predict_streaming( |
| 104 | + prompt="Give me ten interview questions for the role of program manager.", |
| 105 | + **parameters, |
| 106 | + ) |
| 107 | + result = [response for response in responses] |
| 108 | + print(result) |
| 109 | + return result |
| 110 | + |
| 111 | + |
| 112 | +async def async_streaming_prediction() -> str: |
| 113 | + """Async Streaming Text Example with a Large Language Model""" |
| 114 | + |
| 115 | + text_generation_model = TextGenerationModel.from_pretrained("text-bison") |
| 116 | + parameters = { |
| 117 | + "max_output_tokens": 256, |
| 118 | + "top_p": 0.8, |
| 119 | + "top_k": 40, |
| 120 | + } |
| 121 | + |
| 122 | + responses = text_generation_model.predict_streaming_async( |
| 123 | + prompt="Give me ten interview questions for the role of program manager.", |
| 124 | + **parameters, |
| 125 | + ) |
| 126 | + |
| 127 | + result = [response async for response in responses] |
| 128 | + print(result) |
| 129 | + return result |
| 130 | + |
| 131 | + |
| 132 | +def generate(stream=False): |
| 133 | + generation_config = { |
| 134 | + "max_output_tokens": 8192, |
| 135 | + "temperature": 1, |
| 136 | + "top_p": 0.95, |
| 137 | + } |
| 138 | + model = GenerativeModel( |
| 139 | + "gemini-experimental", |
| 140 | + ) |
| 141 | + |
| 142 | + responses = model.generate_content( |
| 143 | + ["I am a software engineer. I enjoy playing video games and reading"], |
| 144 | + generation_config=generation_config, |
| 145 | + stream=stream, |
| 146 | + ) |
| 147 | + |
| 148 | + if stream: |
| 149 | + for res in responses: |
| 150 | + print(res.text) |
| 151 | + else: |
| 152 | + print(responses.text) |
| 153 | + |
| 154 | + |
| 155 | +def image_to_text(stream=False): |
| 156 | + model = GenerativeModel(model_name="gemini-experimental") |
| 157 | + |
| 158 | + response = model.generate_content( |
| 159 | + [ |
| 160 | + Part.from_uri( |
| 161 | + "gs://cloud-samples-data/generative-ai/image/scones.jpg", |
| 162 | + mime_type="image/jpeg", |
| 163 | + ), |
| 164 | + "What is shown in this image?", |
| 165 | + ], |
| 166 | + stream=stream, |
| 167 | + ) |
| 168 | + if stream: |
| 169 | + for res in response: |
| 170 | + print(res.text) |
| 171 | + else: |
| 172 | + print(response.text) |
| 173 | + |
| 174 | + |
| 175 | +def video_to_text(stream=False): |
| 176 | + model = GenerativeModel(model_name="gemini-experimental") |
| 177 | + |
| 178 | + prompt = """ |
| 179 | + Provide a description of the video. |
| 180 | + The description should also contain anything important which people say in the video. |
| 181 | + """ |
| 182 | + |
| 183 | + video_file_uri = "gs://cloud-samples-data/generative-ai/video/pixel8.mp4" |
| 184 | + video_file = Part.from_uri(video_file_uri, mime_type="video/mp4") |
| 185 | + |
| 186 | + contents = [video_file, prompt] |
| 187 | + response = model.generate_content(contents, stream=stream) |
| 188 | + if stream: |
| 189 | + for res in response: |
| 190 | + print(res.text) |
| 191 | + else: |
| 192 | + print(response.text) |
| 193 | + |
| 194 | + |
| 195 | +def audio_to_text(stream=False): |
| 196 | + model = GenerativeModel(model_name="gemini-1.5-flash-001") |
| 197 | + |
| 198 | + prompt = """ |
| 199 | + Please provide a summary for the audio. |
| 200 | + Provide chapter titles, be concise and short, no need to provide chapter summaries. |
| 201 | + Do not make up any information that is not part of the audio and do not be verbose. |
| 202 | + """ |
| 203 | + |
| 204 | + audio_file_uri = "gs://cloud-samples-data/generative-ai/audio/pixel.mp3" |
| 205 | + audio_file = Part.from_uri(audio_file_uri, mime_type="audio/mpeg") |
| 206 | + |
| 207 | + contents = [audio_file, prompt] |
| 208 | + |
| 209 | + response = model.generate_content(contents, stream=stream) |
| 210 | + if stream: |
| 211 | + for res in response: |
| 212 | + print(res.text) |
| 213 | + else: |
| 214 | + print(response.text) |
0 commit comments