Advertisement
Guest User

Untitled

a guest
Nov 27th, 2024
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.48 KB | None | 0 0
  1. import os
  2. import json
  3. import random
  4. from pydantic import BaseModel, Field
  5. import google.generativeai as genai
  6. from google.generativeai.types import GenerationConfig, GenerateContentResponse
  7. from typing import List, Union, Iterator
  8.  
  9. # Set DEBUG to True to enable detailed logging
  10. DEBUG = False
  11.  
  12.  
  13. class Pipe:
  14. class Valves(BaseModel):
  15. GOOGLE_API_KEYS: List[str] = Field(
  16. default=[
  17. "AIzaSE84",
  18. "AIzaSEla",
  19. ]
  20. )
  21. USE_PERMISSIVE_SAFETY: bool = Field(default=False)
  22.  
  23. def __init__(self):
  24. self.id = "google_genai"
  25. self.type = "manifold"
  26. self.name = "Google: "
  27. self.valves = self.Valves(
  28. **{
  29. "GOOGLE_API_KEYS": json.loads(
  30. os.getenv(
  31. "GOOGLE_API_KEYS",
  32. json.dumps(self.Valves().GOOGLE_API_KEYS),
  33. )
  34. ),
  35. "USE_PERMISSIVE_SAFETY": False,
  36. }
  37. )
  38.  
  39. def get_google_models(self):
  40. if not self.valves.GOOGLE_API_KEYS:
  41. return [
  42. {
  43. "id": "error",
  44. "name": "GOOGLE_API_KEYS is not set. Please update the API Keys in the valves.",
  45. }
  46. ]
  47. try:
  48. api_key = random.choice(self.valves.GOOGLE_API_KEYS)
  49. genai.configure(api_key=api_key)
  50. models = genai.list_models()
  51. return [
  52. {
  53. "id": model.name[7:], # remove the "models/" part
  54. "name": model.display_name,
  55. }
  56. for model in models
  57. if "generateContent" in model.supported_generation_methods
  58. if model.name.startswith("models/")
  59. ]
  60. except Exception as e:
  61. if DEBUG:
  62. print(f"Error fetching Google models: {e}")
  63. return [
  64. {"id": "error", "name": f"Could not fetch models from Google: {str(e)}"}
  65. ]
  66.  
  67. def pipes(self) -> List[dict]:
  68. return self.get_google_models()
  69.  
  70. def pipe(self, body: dict) -> Union[str, Iterator[str]]:
  71. if not self.valves.GOOGLE_API_KEYS:
  72. return "Error: GOOGLE_API_KEYS is not set"
  73. try:
  74. api_key = random.choice(self.valves.GOOGLE_API_KEYS)
  75. genai.configure(api_key=api_key)
  76. model_id = body["model"]
  77.  
  78. if model_id.startswith("google_genai."):
  79. model_id = model_id[12:]
  80.  
  81. model_id = model_id.lstrip(".")
  82.  
  83. if not model_id.startswith("gemini-"):
  84. return f"Error: Invalid model name format: {model_id}"
  85.  
  86. messages = body["messages"]
  87. stream = body.get("stream", False)
  88.  
  89. if DEBUG:
  90. print("Incoming body:", str(body))
  91.  
  92. system_message = next(
  93. (msg["content"] for msg in messages if msg["role"] == "system"), None
  94. )
  95.  
  96. contents = []
  97. for message in messages:
  98. if message["role"] != "system":
  99. if isinstance(message.get("content"), list):
  100. parts = []
  101. for content in message["content"]:
  102. if content["type"] == "text":
  103. parts.append({"text": content["text"]})
  104. elif content["type"] == "image_url":
  105. image_url = content["image_url"]["url"]
  106. if image_url.startswith("data:image"):
  107. image_data = image_url.split(",")[1]
  108. parts.append(
  109. {
  110. "inline_data": {
  111. "mime_type": "image/jpeg",
  112. "data": image_data,
  113. }
  114. }
  115. )
  116. else:
  117. parts.append({"image_url": image_url})
  118. contents.append({"role": message["role"], "parts": parts})
  119. else:
  120. contents.append(
  121. {
  122. "role": (
  123. "user" if message["role"] == "user" else "model"
  124. ),
  125. "parts": [{"text": message["content"]}],
  126. }
  127. )
  128.  
  129. if system_message:
  130. contents.insert(
  131. 0,
  132. {"role": "user", "parts": [{"text": f"System: {system_message}"}]},
  133. )
  134.  
  135. if "gemini-1.5" in model_id:
  136. model = genai.GenerativeModel(
  137. model_name=model_id, system_instruction=system_message
  138. )
  139. else:
  140. model = genai.GenerativeModel(model_name=model_id)
  141.  
  142. generation_config = GenerationConfig(
  143. temperature=body.get("temperature", 0.7),
  144. top_p=body.get("top_p", 0.9),
  145. top_k=body.get("top_k", 40),
  146. max_output_tokens=body.get("max_tokens", 8192),
  147. stop_sequences=body.get("stop", []),
  148. )
  149.  
  150. # Safety settings omitted for brevity...
  151. if self.valves.USE_PERMISSIVE_SAFETY:
  152. safety_settings = {
  153. genai.types.HarmCategory.HARM_CATEGORY_HARASSMENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
  154. genai.types.HarmCategory.HARM_CATEGORY_HATE_SPEECH: genai.types.HarmBlockThreshold.BLOCK_NONE,
  155. genai.types.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: genai.types.HarmBlockThreshold.BLOCK_NONE,
  156. genai.types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
  157. }
  158. else:
  159. safety_settings = body.get("safety_settings")
  160.  
  161. if DEBUG:
  162. print("Google API request:")
  163. print(" Model:", model_id)
  164. print(" Contents:", str(contents))
  165. print(" Generation Config:", generation_config)
  166. print(" Safety Settings:", safety_settings)
  167. print(" Stream:", stream)
  168.  
  169. if stream:
  170.  
  171. def stream_generator():
  172. response = model.generate_content(
  173. contents,
  174. generation_config=generation_config,
  175. safety_settings=safety_settings,
  176. stream=True,
  177. )
  178. for chunk in response:
  179. if chunk.text:
  180. yield chunk.text
  181.  
  182. return stream_generator()
  183. else:
  184. response = model.generate_content(
  185. contents,
  186. generation_config=generation_config,
  187. safety_settings=safety_settings,
  188. stream=False,
  189. )
  190. return response.text
  191. except Exception as e:
  192. if DEBUG:
  193. print(f"Error in pipe method: {e}")
  194. return f"Error: {e}"
  195.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement