File size: 1,819 Bytes
2d144e4
 
 
 
 
 
 
 
 
3a856a4
2d144e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Necessary imports
import os
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain.chains import SimpleSequentialChain
from prompts import prompt_transcribe, prompt_command

# API Key
os.environ["OPENAI_API_KEY"] = "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"

# Initializing OpenAI as the large language model
llm = OpenAI(temperature=0.0)


def get_transcription(text):
    # Transcription prompt
    transcribe_prompt = PromptTemplate(
        input_variables=["text"],
        template=prompt_transcribe
    )

    # Creating transcription chain
    sentence_chain = LLMChain(llm=llm,
                              prompt=transcribe_prompt,
                              output_key="sentence")

    return sentence_chain


# Method takes in the transcription chain to link them together with a sequential chain, which is then returned.
def format_command(chain):
    # Prompt Creation
    command = PromptTemplate(
        input_variables=["sentence"],
        template=prompt_command
    )

    # Chain Creation
    command_chain = LLMChain(llm=llm, prompt=command, output_key="output")

    # Initializing chain needed to connect using the parameters
    sentence_chain = chain

    # Connecting the two created chains via the SimpleSequentialChain.
    sentence_command_chain = SimpleSequentialChain(
        chains=[sentence_chain, command_chain], verbose=True
    )

    # Returning the new combined chain
    return sentence_command_chain

def to_file(text):
        f = open("commands.txt", "w")
        f.write(text)
        f.close()

# Method to get the actual formatted command
def get_command(text):
    command = format_command(get_transcription(text))
    to_file(command.run(text))
    return command.run(text)