File size: 1,352 Bytes
8b888b6
a1c4beb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b888b6
 
a1c4beb
 
 
 
8b888b6
a1c4beb
 
ff2fe26
 
a1c4beb
8b888b6
a1c4beb
8b888b6
 
a1c4beb
 
198e41a
a1c4beb
8b888b6
 
 
 
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
import gradio as gr
import yaml
import openai
from pathlib import Path

# Load settings
settings_path = Path(__file__).parent / "settings.yaml"
with open(settings_path, "r") as f:
    settings = yaml.safe_load(f)

# Configure OpenAI client
openai.api_key = settings["openai_api_key"]
openai.api_base = settings["openai_base_url"]

def chatbot(message, history):
    messages = [{"role": "system", "content": "You are a helpful assistant answering question concerning Air Force Personnel Center material. You also try your best to cite the source of your answer."}]
    
    for human, assistant in history:
        messages.append({"role": "user", "content": human})
        messages.append({"role": "assistant", "content": assistant})
    
    messages.append({"role": "user", "content": message})

    client = openai.OpenAI(
        api_key=settings["openai_api_key"],
        base_url=settings["openai_base_url"]
    )

    response = client.chat.completions.create(
        model=settings["openai_model"],
        messages=messages,
        temperature=0
    )

    return response.choices[0].message.content

demo = gr.ChatInterface(
    fn=chatbot,
    examples=["Hello!", "How are you?", "What's the weather like?"],
    title="AFPC Fine-Tuned Chatbot",
    description="AFPC Fine-Tuned Chatbot",
)

if __name__ == "__main__":
    demo.launch()