(self, prompt: str, images: List[str])
| 28 | self.max_tokens = max_tokens |
| 29 | |
| 30 | def get_model_response(self, prompt: str, images: List[str]) -> (bool, str): |
| 31 | content = [ |
| 32 | { |
| 33 | "type": "text", |
| 34 | "text": prompt |
| 35 | } |
| 36 | ] |
| 37 | for img in images: |
| 38 | base64_img = encode_image(img) |
| 39 | content.append({ |
| 40 | "type": "image_url", |
| 41 | "image_url": { |
| 42 | "url": f"data:image/jpeg;base64,{base64_img}" |
| 43 | } |
| 44 | }) |
| 45 | headers = { |
| 46 | "Content-Type": "application/json", |
| 47 | "Authorization": f"Bearer {self.api_key}" |
| 48 | } |
| 49 | payload = { |
| 50 | "model": self.model, |
| 51 | "messages": [ |
| 52 | { |
| 53 | "role": "user", |
| 54 | "content": content |
| 55 | } |
| 56 | ], |
| 57 | "temperature": self.temperature, |
| 58 | "max_tokens": self.max_tokens |
| 59 | } |
| 60 | response = requests.post(self.base_url, headers=headers, json=payload).json() |
| 61 | if "error" not in response: |
| 62 | usage = response["usage"] |
| 63 | prompt_tokens = usage["prompt_tokens"] |
| 64 | completion_tokens = usage["completion_tokens"] |
| 65 | print_with_color(f"Request cost is " |
| 66 | f"${'{0:.2f}'.format(prompt_tokens / 1000 * 0.01 + completion_tokens / 1000 * 0.03)}", |
| 67 | "yellow") |
| 68 | else: |
| 69 | return False, response["error"]["message"] |
| 70 | return True, response["choices"][0]["message"]["content"] |
| 71 | |
| 72 | |
| 73 | class QwenModel(BaseModel): |
nothing calls this directly
no test coverage detected