Skip to content

Commit

Permalink
Merge pull request langchain-ai#2 from ivendrov/patch-1
Browse files Browse the repository at this point in the history
Wrap prompt with "Human" and "Assistant" by default
  • Loading branch information
mikelambert authored Feb 2, 2023
2 parents b21734b + 61a7cac commit a827247
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions langchain/llms/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,37 @@ def _llm_type(self) -> str:
"""Return type of llm."""
return "anthropic"

def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
def _call(self, prompt: str, stop: Optional[List[str]] = None, instruct_mode: Boolean = True) -> str:
"""Call out to Anthropic's completion endpoint.
Will by default act like an instruction-following model, by wrapping the prompt with Human: and Assistant:
If you want to use for chat or few-shot, pass in instruct_mode=False
Args:
prompt: The prompt to pass into the model.
stop: Optional list of stop words to use when generating.
instruct_mode: Whether to emulate an instruction-following model.
Returns:
The string generated by the model.
Example:
.. code-block:: python
response = anthropic("\n\nHuman: Tell me a joke.\n\nAssistant:", [])
response = anthropic("Tell me a joke.")
response = anthropic("\n\nHuman: Tell me a joke.\n\nAssistant:", instruct_mode=False)
"""
if stop is None:
stop = []
# Never want model to invent new turns of Human / Assistant dialog.
stop.extend([anthropic.HUMAN_PROMPT, anthropic.AI_PROMPT])

if instruct_mode:
# Wrap the prompt so it emulates an instruction following model.
prompt = f"{anthropic.HUMAN_PROMPT} prompt{anthropic.AI_PROMPT} Sure, here you go:\n"

response = self.client.completion(
model=self.model, prompt=prompt, stop_sequences=stop, **self._default_params
)
Expand Down

0 comments on commit a827247

Please sign in to comment.