I created a code in python and for some reason when I run it in the interactive tab it works perfectly fine but when I try to run it in the text console it freezes up and then shows me a message that it has lost connection to the server. I’m not asking for any inputs it simply retrieves some data and this never happens in the text console. If anyone has any insight I would appreciate it.
Here’s my code:
import requests
import time
API_KEY = "[you can replace this with an API key by getting a free account at mistral if you want to test it]"
API_URL = "
https://api.mistral.ai/v1/chat/completions"
history_1 = []
history_2 = []
def ask_1(prompt):
global history_1
history_1.append({"role": "user", "content": prompt})
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json",
}
data = {
"model": "ministral-8b-2410",
"messages": history_1,
"temperature": 0.9,
"max_tokens": 200,
}
response = requests.post(API_URL, headers=headers, json=data)
response_1 = response.json()["choices"][0]["message"]["content"]
history_1.append({"role": "assistant", "content": response_1})
return response_1
def ask_2(prompt):
global history_2
history_2.append({"role": "user", "content": prompt})
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json",
}
data = {
"model": "ministral-8b-2410",
"messages": history_2,
"temperature": 0.9,
"max_tokens": 100,
}
response = requests.post(API_URL, headers=headers, json=data)
response_2 = response.json()["choices"][0]["message"]["content"]
history_2.append({"role": "assistant", "content": response_2})
return response_2
response_2 = "Hi, who are you?"
count = 0
while count < 20:
count += 1
print(f"Chat # {count}")
response_1 = ask_1(response_2)
print("AI 1-", response_1)
time.sleep(1)
print("~~~")
response_2 = ask_2(response_1)
print("AI 2-", response_2)
time.sleep(1)
print(
"---------------------------------------------------------------------------------"
)