File size: 1,291 Bytes
caf1faa
 
 
 
 
 
 
 
 
 
 
 
 
bcc8503
 
caf1faa
 
 
bcc8503
caf1faa
 
 
 
 
 
 
 
 
 
 
 
 
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

from typing import List
from typing import Literal
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain.prompts import ChatPromptTemplate
from langchain_core.utils.function_calling import convert_to_openai_function
from langchain.output_parsers.openai_functions import JsonOutputFunctionsParser

class KeywordsOutput(BaseModel):
    """Analyzing the user query to get keywords for a search engine"""
    
    keywords: list = Field(
        description="""
        Generate 1 or 2 relevant keywords from the user query to ask a search engine for scientific research papers. Answer only with English keywords. 
        Do not use special characters or accents.
        
        Example:
        - "What is the impact of deep sea mining ?" -> ["deep sea mining"]
        - "Quel est l'impact de l'exploitation minière en haute mer ?" -> ["deep sea mining"]
        - "How will El Nino be impacted by climate change" -> ["el nino"]
        - "Is climate change a hoax" -> [Climate change","hoax"]
        """
    )


def make_keywords_chain(llm):

    functions = [convert_to_openai_function(KeywordsOutput)]
    llm_functions = llm.bind(functions = functions,function_call={"name":"KeywordsOutput"})

    chain = llm_functions | JsonOutputFunctionsParser()
    return chain