-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added tool usage * added llm * added exception * isort * fix typing errors * remove extra import * fix typing errors
- Loading branch information
1 parent
f15cc2f
commit d5b05de
Showing
9 changed files
with
521 additions
and
610 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .lmm import LMM, LLaVALMM, OpenAILMM, get_lmm | ||
from .emb import Embedder, SentenceTransformerEmb, OpenAIEmb, get_embedder | ||
from .data import DataStore, build_data_store | ||
from .emb import Embedder, OpenAIEmb, SentenceTransformerEmb, get_embedder | ||
from .llm import LLM, OpenAILLM | ||
from .lmm import LMM, LLaVALMM, OpenAILMM, get_lmm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .llm import LLM, OpenAILLM |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import json | ||
from abc import ABC, abstractmethod | ||
from typing import Mapping, cast | ||
|
||
from vision_agent.tools import ( | ||
CHOOSE_PARAMS, | ||
CLIP, | ||
SYSTEM_PROMPT, | ||
GroundingDINO, | ||
GroundingSAM, | ||
ImageTool, | ||
) | ||
|
||
|
||
class LLM(ABC): | ||
@abstractmethod | ||
def generate(self, prompt: str) -> str: | ||
pass | ||
|
||
|
||
class OpenAILLM(LLM): | ||
r"""An LLM class for any OpenAI LLM model.""" | ||
|
||
def __init__(self, model_name: str = "gpt-4-turbo-preview"): | ||
from openai import OpenAI | ||
|
||
self.model_name = model_name | ||
self.client = OpenAI() | ||
|
||
def generate(self, prompt: str) -> str: | ||
response = self.client.chat.completions.create( | ||
model=self.model_name, | ||
messages=[ | ||
{"role": "user", "content": prompt}, | ||
], | ||
) | ||
|
||
return cast(str, response.choices[0].message.content) | ||
|
||
def generate_classifier(self, prompt: str) -> ImageTool: | ||
prompt = CHOOSE_PARAMS.format(api_doc=CLIP.doc, question=prompt) | ||
response = self.client.chat.completions.create( | ||
model=self.model_name, | ||
response_format={"type": "json_object"}, | ||
messages=[ | ||
{"role": "system", "content": SYSTEM_PROMPT}, | ||
{"role": "user", "content": prompt}, | ||
], | ||
) | ||
|
||
params = json.loads(cast(str, response.choices[0].message.content))[ | ||
"Parameters" | ||
] | ||
return CLIP(**cast(Mapping, params)) | ||
|
||
def generate_detector(self, params: str) -> ImageTool: | ||
params = CHOOSE_PARAMS.format(api_doc=GroundingDINO.doc, question=params) | ||
response = self.client.chat.completions.create( | ||
model=self.model_name, | ||
response_format={"type": "json_object"}, | ||
messages=[ | ||
{"role": "system", "content": SYSTEM_PROMPT}, | ||
{"role": "user", "content": params}, | ||
], | ||
) | ||
|
||
params = json.loads(cast(str, response.choices[0].message.content))[ | ||
"Parameters" | ||
] | ||
return GroundingDINO(**cast(Mapping, params)) | ||
|
||
def generate_segmentor(self, params: str) -> ImageTool: | ||
params = CHOOSE_PARAMS.format(api_doc=GroundingSAM.doc, question=params) | ||
response = self.client.chat.completions.create( | ||
model=self.model_name, | ||
response_format={"type": "json_object"}, | ||
messages=[ | ||
{"role": "system", "content": SYSTEM_PROMPT}, | ||
{"role": "user", "content": params}, | ||
], | ||
) | ||
|
||
params = json.loads(cast(str, response.choices[0].message.content))[ | ||
"Parameters" | ||
] | ||
return GroundingSAM(**cast(Mapping, params)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
from .prompts import SYSTEM_PROMPT, CHOOSE_PARAMS | ||
from .tools import ImageTool, CLIP, GroundingDINO, GroundingSAM | ||
from .prompts import CHOOSE_PARAMS, SYSTEM_PROMPT | ||
from .tools import CLIP, GroundingDINO, GroundingSAM, ImageTool |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters