Pamudu13 commited on
Commit
0812198
·
verified ·
1 Parent(s): b444562

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -13
app.py CHANGED
@@ -1,12 +1,14 @@
1
  from flask import Flask, request, jsonify
2
- import openai
3
 
4
  # Initialize Flask application
5
  app = Flask(__name__)
6
 
7
- # Set the base URL for the API endpoint and API key
8
- openai.api_base = "https://api.pawan.krd/unfiltered/v1"
9
- openai.api_key = "pk-IDpTrFbbikTWTSUdJsckGStERwgiZCdaCznbKagvOAVxAftU"
 
 
10
 
11
  history = []
12
  first_message = True
@@ -42,17 +44,22 @@ def chat_bot():
42
  first_message = False
43
  history.append({"role": "user", "content": message})
44
 
45
- # Use the Cosmo-RP model for the completion request
 
 
 
 
 
 
46
  try:
47
- response = openai.Completion.create(
48
- model="Cosmo-RP",
49
- prompt="\n".join([msg['content'] for msg in history]),
50
- max_tokens=150
51
- ).choices[0].text.strip()
52
  except Exception as e:
53
  return jsonify({"error": str(e)}), 500
54
 
55
- history.append({"role": "assistant", "content": response})
56
 
57
  # Trim history to keep within the word limit
58
  trim_history_to_word_limit(history, word_limit)
@@ -60,9 +67,9 @@ def chat_bot():
60
  # Save to a text file
61
  with open(f"{file_name}.txt", 'a', encoding='utf-8') as f:
62
  f.write(f"User: {message}\n")
63
- f.write(f"{chatbot_name}: {response}\n")
64
 
65
- return jsonify({"response": response, "history": history})
66
 
67
  if __name__ == '__main__':
68
  app.run(host='0.0.0.0', port=5000)
 
1
  from flask import Flask, request, jsonify
2
+ import requests
3
 
4
  # Initialize Flask application
5
  app = Flask(__name__)
6
 
7
+ # Set the URL for the Cosmo-RP API
8
+ url = "https://api.pawan.krd/cosmosrp/v1/chat/completions"
9
+ headers = {
10
+ "Content-Type": "application/json"
11
+ }
12
 
13
  history = []
14
  first_message = True
 
44
  first_message = False
45
  history.append({"role": "user", "content": message})
46
 
47
+ # Prepare the data for the Cosmo-RP API call
48
+ api_data = {
49
+ "model": "cosmosrp",
50
+ "messages": [{"role": "system", "content": system_message}] + [{"role": "user", "content": message}]
51
+ }
52
+
53
+ # Make the POST request to the Cosmo-RP API
54
  try:
55
+ response = requests.post(url, headers=headers, json=api_data)
56
+ response.raise_for_status() # Raise an error for bad responses
57
+ api_response = response.json()
58
+ assistant_response = api_response.get("choices", [{}])[0].get("message", {}).get("content", "No response")
 
59
  except Exception as e:
60
  return jsonify({"error": str(e)}), 500
61
 
62
+ history.append({"role": "assistant", "content": assistant_response})
63
 
64
  # Trim history to keep within the word limit
65
  trim_history_to_word_limit(history, word_limit)
 
67
  # Save to a text file
68
  with open(f"{file_name}.txt", 'a', encoding='utf-8') as f:
69
  f.write(f"User: {message}\n")
70
+ f.write(f"{chatbot_name}: {assistant_response}\n")
71
 
72
+ return jsonify({"response": assistant_response, "history": history})
73
 
74
  if __name__ == '__main__':
75
  app.run(host='0.0.0.0', port=5000)