Skip to content

Commit

Permalink
Fix azure OpenAI Embeddings (#121)
Browse files Browse the repository at this point in the history
* update azure openai llm/lmm

* added azure openai sim

* added azure to docs

* added azure to docs

* added import
  • Loading branch information
dillonalaird authored Jun 8, 2024
1 parent 6f6e725 commit 3d92689
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 22 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,12 @@ export AZURE_OPENAI_ENDPOINT="your-endpoint"
You can then run Vision Agent using the Azure OpenAI models:

```python
>>> import vision_agent as va
>>> agent = va.agent.VisionAgent(
>>> planner=va.llm.AzureOpenAILLM(),
>>> coder=va.lmm.AzureOpenAILMM(),
>>> tester=va.lmm.AzureOpenAILMM(),
>>> debugger=va.lmm.AzureOpenAILMM(),
>>> )
import vision_agent as va
agent = va.agent.VisionAgent(
planner=va.llm.AzureOpenAILLM(),
coder=va.lmm.AzureOpenAILMM(),
tester=va.lmm.AzureOpenAILMM(),
debugger=va.lmm.AzureOpenAILMM(),
tool_recommender=va.utils.AzureSim(),
)
```

2 changes: 2 additions & 0 deletions docs/api/llm.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
::: vision_agent.llm.OpenAILLM

::: vision_agent.llm.AzureOpenAILLM
2 changes: 2 additions & 0 deletions docs/api/lmm.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
::: vision_agent.lmm.OpenAILMM

::: vision_agent.lmm.AzureOpenAILMM
16 changes: 9 additions & 7 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,12 @@ export AZURE_OPENAI_ENDPOINT="your-endpoint"
You can then run Vision Agent using the Azure OpenAI models:

```python
>>> import vision_agent as va
>>> agent = va.agent.VisionAgent(
>>> planner=va.llm.AzureOpenAILLM(),
>>> coder=va.lmm.AzureOpenAILMM(),
>>> tester=va.lmm.AzureOpenAILMM(),
>>> debugger=va.lmm.AzureOpenAILMM(),
>>> )
import vision_agent as va
agent = va.agent.VisionAgent(
planner=va.llm.AzureOpenAILLM(),
coder=va.lmm.AzureOpenAILMM(),
tester=va.lmm.AzureOpenAILMM(),
debugger=va.lmm.AzureOpenAILMM(),
tool_recommender=va.utils.AzureSim(),
)
```
2 changes: 1 addition & 1 deletion vision_agent/llm/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def generate_image_qa_tool(self, question: str) -> Callable:
class AzureOpenAILLM(OpenAILLM):
def __init__(
self,
model_name: str = "gpt-4-turbo-preview",
model_name: str = "gpt-4o",
api_key: Optional[str] = None,
api_version: str = "2024-02-01",
azure_endpoint: Optional[str] = None,
Expand Down
9 changes: 7 additions & 2 deletions vision_agent/lmm/lmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,12 @@ def generate_image_qa_tool(self, question: str) -> Callable:
class AzureOpenAILMM(OpenAILMM):
def __init__(
self,
model_name: str = "gpt-4-vision-preview",
model_name: str = "gpt-4o",
api_key: Optional[str] = None,
api_version: str = "2024-02-01",
azure_endpoint: Optional[str] = None,
max_tokens: int = 1024,
json_mode: bool = False,
**kwargs: Any,
):
if not api_key:
Expand All @@ -307,7 +308,11 @@ def __init__(
api_key=api_key, api_version=api_version, azure_endpoint=azure_endpoint
)
self.model_name = model_name
self.max_tokens = max_tokens

if "max_tokens" not in kwargs:
kwargs["max_tokens"] = max_tokens
if json_mode:
kwargs["response_format"] = {"type": "json_object"}
self.kwargs = kwargs


Expand Down
2 changes: 1 addition & 1 deletion vision_agent/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
Logs,
Result,
)
from .sim import Sim, load_sim, merge_sim
from .sim import AzureSim, Sim, load_sim, merge_sim
from .video import extract_frames_from_video
42 changes: 39 additions & 3 deletions vision_agent/utils/sim.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import os
from pathlib import Path
from typing import Dict, List, Optional, Sequence, Union

import numpy as np
import pandas as pd
from openai import Client
from openai import AzureOpenAI, Client, OpenAI
from scipy.spatial.distance import cosine # type: ignore


Expand Down Expand Up @@ -33,9 +34,9 @@ def __init__(
"""
self.df = df
if not api_key:
self.client = Client()
self.client = OpenAI()
else:
self.client = Client(api_key=api_key)
self.client = OpenAI(api_key=api_key)

self.model = model
if "embs" not in df.columns and sim_key is None:
Expand Down Expand Up @@ -78,6 +79,41 @@ def top_k(
return res[[c for c in res.columns if c != "embs"]].to_dict(orient="records")


class AzureSim(Sim):
def __init__(
self,
df: pd.DataFrame,
sim_key: Optional[str] = None,
api_key: Optional[str] = None,
api_version: str = "2024-02-01",
azure_endpoint: Optional[str] = None,
model: str = "text-embedding-3-small",
) -> None:
if not api_key:
api_key = os.getenv("AZURE_OPENAI_API_KEY")
if not azure_endpoint:
azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")

if not api_key:
raise ValueError("Azure OpenAI API key is required.")
if not azure_endpoint:
raise ValueError("Azure OpenAI endpoint is required.")

self.df = df
self.client = AzureOpenAI(
api_key=api_key, api_version=api_version, azure_endpoint=azure_endpoint
)

self.model = model
if "embs" not in df.columns and sim_key is None:
raise ValueError("key is required if no column 'embs' is present.")

if sim_key is not None:
self.df["embs"] = self.df[sim_key].apply(
lambda x: get_embedding(self.client, x, model=self.model)
)


def merge_sim(sim1: Sim, sim2: Sim) -> Sim:
return Sim(pd.concat([sim1.df, sim2.df], ignore_index=True))

Expand Down

0 comments on commit 3d92689

Please sign in to comment.