mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-21 22:29:51 +00:00
Harrison/track token usage (#1382)
Co-authored-by: Zak King <zaking17@gmail.com>
This commit is contained in:
parent
1cd8996074
commit
e178008b75
@ -23,7 +23,7 @@ from tenacity import (
|
|||||||
wait_exponential,
|
wait_exponential,
|
||||||
)
|
)
|
||||||
|
|
||||||
from langchain.llms.base import LLM, BaseLLM
|
from langchain.llms.base import BaseLLM
|
||||||
from langchain.schema import Generation, LLMResult
|
from langchain.schema import Generation, LLMResult
|
||||||
from langchain.utils import get_from_dict_or_env
|
from langchain.utils import get_from_dict_or_env
|
||||||
|
|
||||||
@ -515,7 +515,7 @@ class AzureOpenAI(BaseOpenAI):
|
|||||||
return {**{"engine": self.deployment_name}, **super()._invocation_params}
|
return {**{"engine": self.deployment_name}, **super()._invocation_params}
|
||||||
|
|
||||||
|
|
||||||
class OpenAIChat(LLM, BaseModel):
|
class OpenAIChat(BaseLLM, BaseModel):
|
||||||
"""Wrapper around OpenAI Chat large language models.
|
"""Wrapper around OpenAI Chat large language models.
|
||||||
|
|
||||||
To use, you should have the ``openai`` python package installed, and the
|
To use, you should have the ``openai`` python package installed, and the
|
||||||
@ -621,15 +621,30 @@ class OpenAIChat(LLM, BaseModel):
|
|||||||
|
|
||||||
return _completion_with_retry(**kwargs)
|
return _completion_with_retry(**kwargs)
|
||||||
|
|
||||||
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
|
def _generate(
|
||||||
messages = self.prefix_messages + [{"role": "user", "content": prompt}]
|
self, prompts: List[str], stop: Optional[List[str]] = None
|
||||||
|
) -> LLMResult:
|
||||||
|
if len(prompts) > 1:
|
||||||
|
raise ValueError(f"OpenAIChat only supports single prompts, got {prompts}")
|
||||||
|
messages = self.prefix_messages + [{"role": "user", "content": prompts[0]}]
|
||||||
params: Dict[str, Any] = {**{"model": self.model_name}, **self._default_params}
|
params: Dict[str, Any] = {**{"model": self.model_name}, **self._default_params}
|
||||||
if stop is not None:
|
if stop is not None:
|
||||||
if "stop" in params:
|
if "stop" in params:
|
||||||
raise ValueError("`stop` found in both the input and default params.")
|
raise ValueError("`stop` found in both the input and default params.")
|
||||||
params["stop"] = stop
|
params["stop"] = stop
|
||||||
response = self.completion_with_retry(messages=messages, **params)
|
response = self.completion_with_retry(messages=messages, **params)
|
||||||
return response["choices"][0]["message"]["content"]
|
return LLMResult(
|
||||||
|
generations=[
|
||||||
|
[Generation(text=response["choices"][0]["message"]["content"])]
|
||||||
|
],
|
||||||
|
llm_output={"token_usage": response["usage"]},
|
||||||
|
)
|
||||||
|
|
||||||
|
async def _agenerate(
|
||||||
|
self, prompts: List[str], stop: Optional[List[str]] = None
|
||||||
|
) -> LLMResult:
|
||||||
|
"""Run the LLM on the given prompt and input."""
|
||||||
|
raise NotImplementedError("Async generation not implemented for this LLM.")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _identifying_params(self) -> Mapping[str, Any]:
|
def _identifying_params(self) -> Mapping[str, Any]:
|
||||||
|
Loading…
Reference in New Issue
Block a user