Skip to content

Commit

Permalink
chore(hf-text-gen): extract default params for reusing (langchain-ai#…
Browse files Browse the repository at this point in the history
…7929)

This PR extract common code (default generation params) for
`HuggingFaceTextGenInference`.

Co-authored-by: Junlin Zhou <[email protected]>
  • Loading branch information
edwardzjl and edwardzjl authored Jul 20, 2023
1 parent 54e02e4 commit 812a164
Showing 1 changed file with 23 additions and 35 deletions.
58 changes: 23 additions & 35 deletions langchain/llms/huggingface_text_gen_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class HuggingFaceTextGenInference(LLM):
- _call: Generates text based on a given prompt and stop sequences.
- _acall: Async generates text based on a given prompt and stop sequences.
- _llm_type: Returns the type of LLM.
- _default_params: Returns the default parameters for calling text generation
inference API.
"""

"""
Expand Down Expand Up @@ -123,6 +125,21 @@ def _llm_type(self) -> str:
"""Return type of llm."""
return "huggingface_textgen_inference"

@property
def _default_params(self) -> Dict[str, Any]:
"""Get the default parameters for calling text generation inference API."""
return {
"max_new_tokens": self.max_new_tokens,
"top_k": self.top_k,
"top_p": self.top_p,
"typical_p": self.typical_p,
"temperature": self.temperature,
"repetition_penalty": self.repetition_penalty,
"truncate": self.truncate,
"stop_sequences": self.stop_sequences,
"seed": self.seed,
}

def _call(
self,
prompt: str,
Expand All @@ -138,15 +155,8 @@ def _call(
if not self.stream:
res = self.client.generate(
prompt,
**self._default_params,
stop_sequences=stop,
max_new_tokens=self.max_new_tokens,
top_k=self.top_k,
top_p=self.top_p,
typical_p=self.typical_p,
temperature=self.temperature,
repetition_penalty=self.repetition_penalty,
truncate=self.truncate,
seed=self.seed,
**kwargs,
)
# remove stop sequences from the end of the generated text
Expand All @@ -163,15 +173,9 @@ def _call(
run_manager.on_llm_new_token, verbose=self.verbose
)
params = {
**self._default_params,
"stop_sequences": stop,
"max_new_tokens": self.max_new_tokens,
"top_k": self.top_k,
"top_p": self.top_p,
"typical_p": self.typical_p,
"temperature": self.temperature,
"repetition_penalty": self.repetition_penalty,
"truncate": self.truncate,
"seed": self.seed,
**kwargs,
}
text = ""
for res in self.client.generate_stream(prompt, **params):
Expand Down Expand Up @@ -204,15 +208,8 @@ async def _acall(
if not self.stream:
res = await self.async_client.generate(
prompt,
**self._default_params,
stop_sequences=stop,
max_new_tokens=self.max_new_tokens,
top_k=self.top_k,
top_p=self.top_p,
typical_p=self.typical_p,
temperature=self.temperature,
repetition_penalty=self.repetition_penalty,
truncate=self.truncate,
seed=self.seed,
**kwargs,
)
# remove stop sequences from the end of the generated text
Expand All @@ -229,17 +226,8 @@ async def _acall(
run_manager.on_llm_new_token, verbose=self.verbose
)
params = {
**{
"stop_sequences": stop,
"max_new_tokens": self.max_new_tokens,
"top_k": self.top_k,
"top_p": self.top_p,
"typical_p": self.typical_p,
"temperature": self.temperature,
"repetition_penalty": self.repetition_penalty,
"truncate": self.truncate,
"seed": self.seed,
},
**self._default_params,
"stop_sequences": stop,
**kwargs,
}
text = ""
Expand Down

0 comments on commit 812a164

Please sign in to comment.