Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

together: fix chat model and embedding classes #21353

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libs/partners/together/langchain_together/chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def _llm_type(self) -> str:
together_api_key: Optional[SecretStr] = Field(default=None, alias="api_key")
"""Automatically inferred from env are `TOGETHER_API_KEY` if not provided."""
together_api_base: Optional[str] = Field(
default="https://api.together.ai/v1/chat/completions", alias="base_url"
default="https://api.together.ai/v1/", alias="base_url"
)

@root_validator()
Expand Down
24 changes: 14 additions & 10 deletions libs/partners/together/langchain_together/embeddings.py
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Nutlope just want to make sure I'm not messing anything up by removing the logic appending -query and -passage to the models! It doesn't seem to work with the default togethercomputer models.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right! These shouldn't have been there, thanks

Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class TogetherEmbeddings(BaseModel, Embeddings):
client: Any = Field(default=None, exclude=True) #: :meta private:
async_client: Any = Field(default=None, exclude=True) #: :meta private:
model: str = "togethercomputer/m2-bert-80M-8k-retrieval"
"""Embeddings model name to use. Do not add suffixes like `-query` and `-passage`.
"""Embeddings model name to use.
Instead, use 'togethercomputer/m2-bert-80M-8k-retrieval' for example.
"""
dimensions: Optional[int] = None
Expand All @@ -62,7 +62,7 @@ class TogetherEmbeddings(BaseModel, Embeddings):
together_api_key: Optional[SecretStr] = Field(default=None, alias="api_key")
"""API Key for Solar API."""
together_api_base: str = Field(
default="https://api.together.ai/v1/embeddings", alias="base_url"
default="https://api.together.ai/v1/", alias="base_url"
)
"""Endpoint URL to use."""
embedding_ctx_length: int = 4096
Expand Down Expand Up @@ -166,21 +166,25 @@ def validate_environment(cls, values: Dict) -> Dict:
"default_query": values["default_query"],
}
if not values.get("client"):
sync_specific = {"http_client": values["http_client"]}
sync_specific = (
{"http_client": values["http_client"]} if values["http_client"] else {}
)
values["client"] = openai.OpenAI(
**client_params, **sync_specific
).embeddings
if not values.get("async_client"):
async_specific = {"http_client": values["http_async_client"]}
async_specific = (
{"http_client": values["http_async_client"]}
if values["http_async_client"]
else {}
)
values["async_client"] = openai.AsyncOpenAI(
**client_params, **async_specific
).embeddings
return values

@property
def _invocation_params(self) -> Dict[str, Any]:
self.model = self.model.replace("-query", "").replace("-passage", "")

params: Dict = {"model": self.model, **self.model_kwargs}
if self.dimensions is not None:
params["dimensions"] = self.dimensions
Expand All @@ -197,7 +201,7 @@ def embed_documents(self, texts: List[str]) -> List[List[float]]:
"""
embeddings = []
params = self._invocation_params
params["model"] = params["model"] + "-passage"
params["model"] = params["model"]

for text in texts:
response = self.client.create(input=text, **params)
Expand All @@ -217,7 +221,7 @@ def embed_query(self, text: str) -> List[float]:
Embedding for the text.
"""
params = self._invocation_params
params["model"] = params["model"] + "-query"
params["model"] = params["model"]

response = self.client.create(input=text, **params)

Expand All @@ -236,7 +240,7 @@ async def aembed_documents(self, texts: List[str]) -> List[List[float]]:
"""
embeddings = []
params = self._invocation_params
params["model"] = params["model"] + "-passage"
params["model"] = params["model"]

for text in texts:
response = await self.async_client.create(input=text, **params)
Expand All @@ -256,7 +260,7 @@ async def aembed_query(self, text: str) -> List[float]:
Embedding for the text.
"""
params = self._invocation_params
params["model"] = params["model"] + "-query"
params["model"] = params["model"]

response = await self.async_client.create(input=text, **params)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ def chat_model_class(self) -> Type[BaseChatModel]:
@pytest.fixture
def chat_model_params(self) -> dict:
return {
"model": "meta-llama/Llama-3-8b-chat-hf",
"model": "mistralai/Mistral-7B-Instruct-v0.1",
}
Loading