Skip to content

Commit

Permalink
Add extract_answer
Browse files Browse the repository at this point in the history
  • Loading branch information
jncraton committed May 8, 2023
1 parent 2b00c64 commit 0098f97
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion languagemodels/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .languagemodels import do, chat, is_positive, match, search
from .languagemodels import do, chat, is_positive, match, search, extract_answer
17 changes: 17 additions & 0 deletions languagemodels/languagemodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,23 @@ def search(topic):
return summary


def extract_answer(question, context):
"""Extract an answer to a question from a provided context
>>> extract_answer("What color is the ball?", "There is a green ball and a red box")
'green'
>>> extract_answer("Who created Python?", search('Python'))
'Guido van Rossum'
"""

model = "distilbert-base-cased-distilled-squad"

if model not in modelcache:
modelcache[model] = pipeline("question-answering", model=model)

return modelcache[model](question, context)["answer"]


def is_positive(doc):
"""Returns true of a supplied string is positive
Expand Down
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ True

>>> lm.search('Chemistry')
'Chemistry is the scientific study...

>>> lm.extract_answer("What color is the ball?", "There is a green ball and a red box")
'green'
```

0 comments on commit 0098f97

Please sign in to comment.