-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathopenai_itt.py
47 lines (37 loc) · 1.27 KB
/
openai_itt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
OpenAIImageToText Module
"""
from langchain_core.messages import HumanMessage
from langchain_openai import ChatOpenAI
class OpenAIImageToText(ChatOpenAI):
"""
A wrapper for the OpenAIImageToText class that provides default configuration
and could be extended with additional methods if needed.
Args:
llm_config (dict): Configuration parameters for the language model.
max_tokens (int): The maximum number of tokens to generate.
"""
def __init__(self, llm_config: dict):
super().__init__(**llm_config, max_tokens=256)
def run(self, image_url: str) -> str:
"""
Runs the image-to-text conversion using the provided image URL.
Args:
image_url (str): The URL of the image to convert.
Returns:
str: The text description of the image.
"""
message = HumanMessage(
content=[
{"type": "text", "text": "What is this image showing"},
{
"type": "image_url",
"image_url": {
"url": image_url,
"detail": "auto",
},
},
]
)
result = self.invoke([message]).content
return result