File size: 2,298 Bytes
7bc6f72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
from swarmauri.standard.llms.concrete.GroqModel import GroqModel
from swarmauri.standard.messages.concrete.SystemMessage import SystemMessage
from swarmauri.standard.agents.concrete.SimpleConversationAgent import SimpleConversationAgent
from swarmauri.standard.conversations.concrete.MaxSystemContextConversation import MaxSystemContextConversation
import gradio as gr
import os
from dotenv import load_dotenv
# Fetch the API key from environment variables

load_dotenv()

# Fetch the API key from environment variables
API_KEY = os.getenv("GROQ_API_KEY")


# Initialize the GroqModel with the API key to access allowed models
llm = GroqModel(api_key=API_KEY)

# Get the available models from the llm instance
allowed_models = llm.allowed_models

# Initialize a MaxSystemContextConversation instance
conversation = MaxSystemContextConversation()

# Define a function to dynamically change the model based on dropdown input
def load_model(selected_model):
    return GroqModel(api_key=API_KEY, name=selected_model)

# Define the function to interact with the agent
def converse(input_text, history, system_context, model_name):
    print(f"System context: {system_context}")
    print(f"Selected model: {model_name}")

    # Initialize the model dynamically based on user selection
    llm = load_model(model_name)

    # Initialize the agent with the new model
    agent = SimpleConversationAgent(llm=llm, conversation=conversation)

    # Set the system context for the agent
    agent.conversation.system_context = SystemMessage(content=system_context)
        # Ensure input text is a string
    input_text = str(input_text)

    print(conversation.history)

    # Execute the input command with the agent
    result = agent.exec(input_text)

    print(result, type(result))

    # Return the result as a string
    return str(result)

# Set up the Gradio ChatInterface with a dropdown for model selection
interface = gr.ChatInterface(
    fn=converse,
    additional_inputs=[
        gr.Textbox(label="System Context"),
        gr.Dropdown(label="Model Name", choices=allowed_models, value=allowed_models[0])
    ],
    title="A system context conversation",
    description="Interact with the agent using a selected model and system context."
)

# Start the Gradio interface
interface.launch()