File size: 2,325 Bytes
7c01a62 8c4b624 7c01a62 8c4b624 7c01a62 8c4b624 06aab8b 7c01a62 06aab8b 7c01a62 06aab8b 7c01a62 8c4b624 7c01a62 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
from typing import Dict, Any
from langchain.prompts import PromptTemplate
from langchain.tools import Tool
from langchain_openai import ChatOpenAI
from langchain.chains import LLMChain
tool_llm = ChatOpenAI(model='gpt-3.5-turbo', temperature=0)
# Tools
# Get Animals From Description
LIKELY_ANIMALS_PROMPT = """Given the following description of an animal, discuss what possible animals it could be referring to and reasons why.
Animal Description:
{animal_description}
Possible Animals:
"""
likely_animals_prompt = PromptTemplate(
input_variables=['animal_description'],
template=LIKELY_ANIMALS_PROMPT
)
likely_animals_chain = LLMChain(llm=tool_llm, prompt=likely_animals_prompt)
def get_likely_animals(description: str) -> str:
"""Provides animals from a description"""
return likely_animals_chain.invoke(input={'animal_description': description})['text']
# Animal Match Tool
ANIMAL_MATCH_PROMPT = """\
Consider whether the following animal matches with the description provided. Provide your logic for reaching the conclusion.
ANIMAL:
{animal}
Description:
{description}
Your Thoughts:
"""
animal_match_template = PromptTemplate(
input_variables=['animal', 'description'],
template=ANIMAL_MATCH_PROMPT
)
animal_match_tool = LLMChain(llm=tool_llm, prompt=likely_animals_prompt)
def does_animal_match_description(animal: str, description: str) -> dict[str, Any]:
"""Given an animal and a description, consider whether the animal matches that description"""
return animal_match_tool.invoke(input={"animal": animal, "description": description})['text']
animal_tools = [
Tool(
name='get_likely_animals',
func=get_likely_animals,
description='used to get a list of potential animals corresponding to a description of an animal'
)
]
VOTE_EXTRACTION_PROMPT = """Extract the name of the player being voted for, from the following statement:
{statement}"""
vote_extraction_template = PromptTemplate(
input_variables=['statement'],
template=VOTE_EXTRACTION_PROMPT
)
vote_extraction_chain = LLMChain(llm=tool_llm, prompt=vote_extraction_template)
def extract_vote(statement: str) -> str:
"""Extract the name of the player being voted for from the statement"""
return vote_extraction_chain.invoke(input={"statement":statement})['text'] |