-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllm_module.py
65 lines (47 loc) · 2.02 KB
/
llm_module.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
63
64
65
from langchain.llms import Cohere
import streamlit as st
import random
import json
# Load environment variables
cohere_api_key = st.secrets["COHERE_API_KEY"]
# Initialize Cohere model
cohere = Cohere(model='command-nightly', temperature=0.9, max_tokens=100)
# Function to convert generated text to JSON
def convert_text_to_json(text, section, subsection_number, subsection_name, instruction, language, start):
data = []
sentences = text.strip().split('\n')
for exercise_number, sentence_block in enumerate(sentences, start=start):
sentences_lines = sentence_block.split('.')
sentence = sentences_lines[0]
answer = sentences_lines[1] if len(sentences_lines) > 1 else "?"
item = {
'exercise_number': exercise_number,
'sentence': sentence,
'answer': answer
}
data.append(item)
section_data = {
'section': section,
'subsection_number': subsection_number,
'subsection_name': subsection_name,
'instruction': instruction,
'language': language,
'exercises': data
}
json_data = json.dumps([section_data], indent=2, ensure_ascii=False)
return json_data
# Loop through the exercises and generate new sentences and answers
def generate_additional_sentences(exercises, section, subsection_number, subsection_name, instruction, language, start):
example1 = random.sample(exercises, k=1)[-1]
example2 = random.sample(exercises, k=1)[-1]
text = f"""
Example 1:
{example1.get("sentence", None)} {example1.get("answer", None)}
Example 2:
{example2.get("sentence", None)} {example2.get("answer", None)}
Generate one example following the same structure. Exclude the introductory and concluding sentences.
"""
new_sentences = cohere(text)
new_sentences_json = convert_text_to_json(new_sentences, section, subsection_number,
subsection_name, instruction, language=language, start=start)
return json.loads(new_sentences_json)