You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.5 KiB
66 lines
1.5 KiB
1 year ago
|
import os
|
||
1 year ago
|
|
||
1 year ago
|
from dotenv import load_dotenv
|
||
1 year ago
|
|
||
1 year ago
|
from swarms import (
|
||
|
OpenAIChat,
|
||
|
Conversation,
|
||
1 year ago
|
detect_markdown,
|
||
|
extract_code_from_markdown,
|
||
1 year ago
|
)
|
||
|
|
||
1 year ago
|
from swarms.tools.code_executor import CodeExecutor
|
||
|
|
||
1 year ago
|
conv = Conversation(
|
||
1 year ago
|
autosave=False,
|
||
1 year ago
|
time_enabled=True,
|
||
|
)
|
||
1 year ago
|
|
||
1 year ago
|
# Load the environment variables
|
||
|
load_dotenv()
|
||
|
|
||
|
# Get the API key from the environment
|
||
|
api_key = os.environ.get("OPENAI_API_KEY")
|
||
|
|
||
|
# Initialize the language model
|
||
1 year ago
|
llm = OpenAIChat(openai_api_key=api_key)
|
||
1 year ago
|
|
||
1 year ago
|
|
||
1 year ago
|
# Run the language model in a loop
|
||
1 year ago
|
def interactive_conversation(llm, iters: int = 10):
|
||
1 year ago
|
conv = Conversation()
|
||
1 year ago
|
for i in range(iters):
|
||
1 year ago
|
user_input = input("User: ")
|
||
|
conv.add("user", user_input)
|
||
1 year ago
|
|
||
1 year ago
|
if user_input.lower() == "quit":
|
||
|
break
|
||
1 year ago
|
|
||
1 year ago
|
task = (
|
||
|
conv.return_history_as_string()
|
||
|
) # Get the conversation history
|
||
1 year ago
|
|
||
|
# Run the language model
|
||
1 year ago
|
out = llm(task)
|
||
|
conv.add("assistant", out)
|
||
|
print(
|
||
1 year ago
|
f"Assistant: {out}",
|
||
1 year ago
|
)
|
||
1 year ago
|
|
||
|
# Code Interpreter
|
||
|
if detect_markdown(out):
|
||
|
code = extract_code_from_markdown(out)
|
||
|
if code:
|
||
|
print(f"Code: {code}")
|
||
|
executor = CodeExecutor()
|
||
|
out = executor.run(code)
|
||
|
conv.add("assistant", out)
|
||
|
# print(f"Assistant: {out}")
|
||
|
|
||
1 year ago
|
conv.display_conversation()
|
||
1 year ago
|
# conv.export_conversation("conversation.txt")
|
||
1 year ago
|
|
||
|
|
||
1 year ago
|
# Replace with your LLM instance
|
||
|
interactive_conversation(llm)
|