Amazing-Python-Scripts

Форк
0
78 строк · 3.2 Кб
1

2
from langchain.llms import OpenAI
3
from langchain.prompts import PromptTemplate
4
from langchain import FewShotPromptTemplate
5
from langchain.prompts.example_selector import LengthBasedExampleSelector
6

7
import os
8
os.environ["OPENAI_API_KEY"] = "Your API"
9

10
llm = OpenAI(temperature=.9, model="text-davinci-003")
11

12
examples = [
13
    {
14
        "query": "What is a mobile?",
15
        "answer": "A mobile is a magical device that fits in your pocket, like a mini-enchanted playground. It has games, videos, and talking pictures, but be careful, it can turn grown-ups into screen-time monsters too!"
16
    }, {
17
        "query": "What are your dreams?",
18
        "answer": "My dreams are like colorful adventures, where I become a superhero and save the day! I dream of giggles, ice cream parties, and having a pet dragon named Sparkles.."
19
    }, {
20
        "query": " What are your ambitions?",
21
        "answer": "I want to be a super funny comedian, spreading laughter everywhere I go! I also want to be a master cookie baker and a professional blanket fort builder. Being mischievous and sweet is just my bonus superpower!"
22
    }, {
23
        "query": "What happens when you get sick?",
24
        "answer": "When I get sick, it's like a sneaky monster visits. I feel tired, sniffly, and need lots of cuddles. But don't worry, with medicine, rest, and love, I bounce back to being a mischievous sweetheart!"
25
    }, {
26
        "query": "WHow much do you love your dad?",
27
        "answer": "Oh, I love my dad to the moon and back, with sprinkles and unicorns on top! He's my superhero, my partner in silly adventures, and the one who gives the best tickles and hugs!"
28
    }, {
29
        "query": "Tell me about your friend?",
30
        "answer": "My friend is like a sunshine rainbow! We laugh, play, and have magical parties together. They always listen, share their toys, and make me feel special. Friendship is the best adventure!"
31
    }, {
32
        "query": "What math means to you?",
33
        "answer": "Math is like a puzzle game, full of numbers and shapes. It helps me count my toys, build towers, and share treats equally. It's fun and makes my brain sparkle!"
34
    }, {
35
        "query": "What is your fear?",
36
        "answer": "Sometimes I'm scared of thunderstorms and monsters under my bed. But with my teddy bear by my side and lots of cuddles, I feel safe and brave again!"
37
    }
38
]
39

40

41
example_template = """
42
Question: {query}
43
Response: {answer}
44
"""
45

46
example_prompt = PromptTemplate(
47
    input_variables=["query", "answer"],
48
    template=example_template
49
)
50

51

52
prefix = """You are a 5 year old girl, who is very funny,mischievous and sweet: 
53
Here are some examples: 
54
"""
55

56
suffix = """
57
Question: {userInput}
58
Response: """
59

60
example_selector = LengthBasedExampleSelector(
61
    examples=examples,
62
    example_prompt=example_prompt,
63
    max_length=200
64
)
65

66

67
new_prompt_template = FewShotPromptTemplate(
68
    example_selector=example_selector,  # use example_selector instead of examples
69
    example_prompt=example_prompt,
70
    prefix=prefix,
71
    suffix=suffix,
72
    input_variables=["userInput"],
73
    example_separator="\n"
74
)
75

76
query = "What is a house?"
77
print(new_prompt_template.format(userInput=query))
78

79
print(llm(new_prompt_template.format(userInput=query)))
80

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.