-
Notifications
You must be signed in to change notification settings - Fork 2
/
few_shot_util.py
62 lines (46 loc) · 1.58 KB
/
few_shot_util.py
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
import json
from typing import List
import outlines
from env_manager import llm_class
from logger import logger
from utils import get_from_env_or_config, convert_chat_messages
temperature = float(get_from_env_or_config("llm", "temperature"))
chatClient = llm_class.get_client(temperature=temperature)
instructions = get_from_env_or_config('few_shot_config', 'instructions', None)
examples = json.loads(get_from_env_or_config('few_shot_config', 'examples', None))
@outlines.prompt
def few_shots(instructions, examples):
"""{{ instructions }}
Examples
--------
{% for item_group, items in examples.items() %}
{% if items %}
{% for item in items %}
Q: {{ item.question }}
A: {{ item.answer }}
{% if not loop.last %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
Question
--------
Q: user_question
A:
"""
prompt = few_shots(instructions, examples)
def invokeLLM(question):
system_rules = prompt.replace("user_question", question)
logger.debug("system_rules::: ", system_rules)
response = call_chat_model(
messages=[
{"role": "system", "content": system_rules},
{"role": "user", "content": question}
]
)
logger.debug("response::: ", response)
return response
def call_chat_model(messages: List[dict]) -> str:
converted_messages = convert_chat_messages(messages)
response = chatClient.invoke(input=converted_messages)
return response.content