mangoman7002 commited on
Commit
9dfac8f
·
1 Parent(s): ae867fa

New Uodate

Browse files
Files changed (3) hide show
  1. api.py +39 -0
  2. chat.py +6 -1
  3. requirements.txt +1 -1
api.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import mdtex2html
2
+ from flask import Flask, request, jsonify
3
+ from chat import converse
4
+ import json
5
+
6
+ app = Flask(__name__)
7
+
8
+ @app.route("/", methods=['GET', 'POST'])
9
+ def home():
10
+ try:
11
+ par = request.get_json()
12
+ if not par:
13
+ return jsonify({"error": "Invalid JSON input"}), 400
14
+
15
+ conversation = par.get('conversation')
16
+ provider = par.get('provider')
17
+ model = par.get('model')
18
+ api = par.get('api')
19
+
20
+ if not all([conversation, provider, model, api]):
21
+ return jsonify({"error": "Missing required parameters"}), 400
22
+
23
+ load = json.loads(converse(conversation, provider, model, api))
24
+ load['DirectResult'] = load['content']
25
+
26
+ try:
27
+ load['content'] = mdtex2html.convert(load['content'])
28
+ except Exception as e:
29
+ return jsonify({"error": f"Error converting content: {str(e)}"}), 500
30
+
31
+ toreturn = json.dumps(load, indent=4)
32
+ # print(toreturn)
33
+ return toreturn, 200
34
+
35
+ except Exception as e:
36
+ return jsonify({"error": str(e)}), 500
37
+
38
+ if __name__ == "__main__":
39
+ app.run(host='0.0.0.0', debug=True, port=1777)
chat.py CHANGED
@@ -4,15 +4,20 @@ from langchain_groq import ChatGroq
4
  from langchain.chains import ConversationChain
5
  from langchain_google_genai import ChatGoogleGenerativeAI
6
  from langchain_core.output_parsers import JsonOutputParser
 
7
  def langchainConversation(conversation):
8
  prompts = []
9
  for message in conversation:
10
  prompts.append((message['role'],message['context']))
11
  chat_template = ChatPromptTemplate.from_messages(prompts)
12
  return chat_template.format_messages()
13
- def converse(conversation,provider,model,key):
14
  if(provider=='groq'):
15
  chat = ChatGroq(temperature=0, groq_api_key=key, model_name=model)
16
  elif(provider=='gemini'):
17
  chat = ChatGoogleGenerativeAI(model=model,google_api_key=key)
 
 
 
 
18
  return json.dumps(json.loads(chat.invoke(langchainConversation(conversation)).json()),indent=4)
 
4
  from langchain.chains import ConversationChain
5
  from langchain_google_genai import ChatGoogleGenerativeAI
6
  from langchain_core.output_parsers import JsonOutputParser
7
+ from langchain_cohere import ChatCohere
8
  def langchainConversation(conversation):
9
  prompts = []
10
  for message in conversation:
11
  prompts.append((message['role'],message['context']))
12
  chat_template = ChatPromptTemplate.from_messages(prompts)
13
  return chat_template.format_messages()
14
+ def converse(conversation,provider,model,key,other:dict={}):
15
  if(provider=='groq'):
16
  chat = ChatGroq(temperature=0, groq_api_key=key, model_name=model)
17
  elif(provider=='gemini'):
18
  chat = ChatGoogleGenerativeAI(model=model,google_api_key=key)
19
+ elif(provider=='cohere'):
20
+ chat = ChatCohere(model=model,cohere_api_key=key)
21
+ else:
22
+ return json.dumos({'content':'unspported Provider'})
23
  return json.dumps(json.loads(chat.invoke(langchainConversation(conversation)).json()),indent=4)
requirements.txt CHANGED
@@ -6,4 +6,4 @@ langchain-community
6
  jq
7
  flask
8
  mdtex2html
9
- protobuf
 
6
  jq
7
  flask
8
  mdtex2html
9
+ langchain_cohere