File size: 3,288 Bytes
eb72193
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a0a140
eb72193
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a0a140
eb72193
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
from langchain.chains.combine_documents.refine import RefineDocumentsChain
from langchain.chains.qa_with_sources import load_qa_with_sources_chain

from template import template, template_many, template_many_out_put
from langchain import LLMChain
from langchain.llms import OpenAI, AzureOpenAI
from langchain.output_parsers import StructuredOutputParser, ResponseSchema
from langchain.prompts import PromptTemplate

def LLMChain_test_outputs(context, result_count):

    llm = OpenAI(model_name="text-davinci-003", engine="text-davinci-003",temperature=0)
    prompt = PromptTemplate(template=template, input_variables=['context'])
    chain = LLMChain(llm=llm, prompt=prompt)
    return chain.run(
        {
            'context': context,
        }
    )

def LLMChain_test(context):
    llm = OpenAI(model_name="text-davinci-003", engine="text-davinci-003",temperature=0)
    prompt = PromptTemplate(template=template_many, input_variables=['context','positive', 'negative','indifference'])
    chain = LLMChain(llm=llm, prompt=prompt)
    return chain.run(
        {
            'context': context,
            'positive': '正面',
            'negative': '负面',
            'indifference': '无关'
        }
    )

def LLMChain_test_many(context,count):
    llm = OpenAI(model_name="text-davinci-003", engine="text-davinci-003", temperature=0,max_tokens=3000,verbose=True)
    prompt = PromptTemplate(template=template_many,
                            input_variables=['count','context', 'positive', 'negative', 'indifference'],
                            )
    chain = LLMChain(llm=llm, prompt=prompt)
    return chain.run(
        {
            'count': count,
            'context': context,
            'positive': '正面',
            'negative': '负面',
            'indifference': '无关'
        })

def LLMChain_test_many_output(context,count):
    llm = OpenAI(model_name="text-davinci-003", engine="text-davinci-003", temperature=0,max_tokens=3000,verbose=True)

    response_schemas = [
        ResponseSchema(name="index", description="句子的序号作为数组的下标"),
        ResponseSchema(name="emotion", description="每个句子的情绪,枚举值:正面、负面、无关"),
        ResponseSchema(name="description", description="对每个句子的总结"),
        ResponseSchema(name="count", description="单独key整体的正面、负面、无关的条数,并且只对整体"),
        ResponseSchema(name="summary", description="给出所有句子整体的中文简体总结")
    ]

    # 初始化解析器
    output_parser = StructuredOutputParser.from_response_schemas(response_schemas)

    format_instructions = output_parser.get_format_instructions()

    prompt = PromptTemplate(template=template_many_out_put,
                            input_variables=['count','context', 'positive', 'negative', 'indifference'],
                            partial_variables={"format_instructions": format_instructions},)

    promptValue = prompt.format(**{
            'count': count,
            'context': context,
            'positive': '正面',
            'negative': '负面',
            'indifference': '无关'
        })
    llm_output = llm(promptValue)
    # return output_parser.parse(llm_output)
    return llm_output