mirror of
https://github.com/hwchase17/langchain.git
synced 2025-04-27 19:46:55 +00:00
ape example
This commit is contained in:
parent
f23b3ceb49
commit
3561f18e66
111
examples/ape.ipynb
Normal file
111
examples/ape.ipynb
Normal file
@ -0,0 +1,111 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "081420ea",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"words = [\"sane\", \"direct\", \"informally\", \"unpopular\", \"subtractive\", \"nonresidential\",\n",
|
||||
" \"inexact\", \"uptown\", \"incomparable\", \"powerful\", \"gaseous\", \"evenly\", \"formality\",\n",
|
||||
" \"deliberately\", \"off\"]\n",
|
||||
"antonyms = [\"insane\", \"indirect\", \"formally\", \"popular\", \"additive\", \"residential\",\n",
|
||||
" \"exact\", \"downtown\", \"comparable\", \"powerless\", \"solid\", \"unevenly\", \"informality\",\n",
|
||||
" \"accidentally\", \"on\"]\n",
|
||||
"data = (words, antonyms)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "6c7c00b7",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"examples = [f\"INPUT: {i}\\nOUTPUT: {j}\" for i, j in zip(words, antonyms)]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "70d4ea31",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.chains.ape.base import APEChain\n",
|
||||
"from langchain.llms import OpenAI"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "f64829c4",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"llm = OpenAI()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "0663e721",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"chain = APEChain(llm=llm)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"id": "4d65f1ee",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"' reverse the input.'"
|
||||
]
|
||||
},
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"chain.ape(examples)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "3bd0bd06",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.7.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
0
langchain/chains/ape/__init__.py
Normal file
0
langchain/chains/ape/__init__.py
Normal file
47
langchain/chains/ape/base.py
Normal file
47
langchain/chains/ape/base.py
Normal file
@ -0,0 +1,47 @@
|
||||
from typing import Dict, List
|
||||
|
||||
from langchain.chains.base import Chain
|
||||
from pydantic import BaseModel, Extra
|
||||
from langchain.llms.base import LLM
|
||||
from langchain.chains.llm import LLMChain
|
||||
from langchain.chains.ape.prompt import PROMPT
|
||||
|
||||
|
||||
class APEChain(Chain, BaseModel):
|
||||
|
||||
llm: LLM
|
||||
|
||||
input_key: str = "code" #: :meta private:
|
||||
output_key: str = "output" #: :meta private:
|
||||
|
||||
class Config:
|
||||
"""Configuration for this pydantic object."""
|
||||
|
||||
extra = Extra.forbid
|
||||
arbitrary_types_allowed = True
|
||||
|
||||
@property
|
||||
def input_keys(self) -> List[str]:
|
||||
"""Expect input key.
|
||||
|
||||
:meta private:
|
||||
"""
|
||||
return [self.input_key]
|
||||
|
||||
@property
|
||||
def output_keys(self) -> List[str]:
|
||||
"""Return output key.
|
||||
|
||||
:meta private:
|
||||
"""
|
||||
return [self.output_key]
|
||||
|
||||
def _call(self, inputs: Dict[str, str]) -> Dict[str, str]:
|
||||
chain = LLMChain(llm=self.llm, prompt=PROMPT)
|
||||
output = chain.run(inputs[self.input_key])
|
||||
return {self.output_key: output}
|
||||
|
||||
def ape(self, examples: List[str]) -> str:
|
||||
combined_examples = "\n\n".join(examples)
|
||||
return self.run(combined_examples)
|
||||
|
9
langchain/chains/ape/prompt.py
Normal file
9
langchain/chains/ape/prompt.py
Normal file
@ -0,0 +1,9 @@
|
||||
# flake8: noqa
|
||||
from langchain.prompts.prompt import Prompt
|
||||
_TEMPLATE = """I gave a friend an instruction. Based on the instruction they produced the following input-output pairs:
|
||||
|
||||
{examples}
|
||||
|
||||
The instruction was to"""
|
||||
|
||||
PROMPT = Prompt(input_variables=["examples"], template=_TEMPLATE)
|
Loading…
Reference in New Issue
Block a user