forked from React-Group/interstellar_ai
Compare commits
No commits in common. "6a1aabcbe8223f03c4ef7bbaa199488d9e44b568" and "2a5de3210800d0d679b185b55c4ce0ea9cbef45d" have entirely different histories.
6a1aabcbe8
...
2a5de32108
3 changed files with 2 additions and 34 deletions
21
py/ai.py
21
py/ai.py
|
@ -1,6 +1,5 @@
|
||||||
from mistralai import Mistral
|
from mistralai import Mistral
|
||||||
from openai import OpenAI
|
from openai import OpenAI
|
||||||
import anthropic
|
|
||||||
import ollama
|
import ollama
|
||||||
|
|
||||||
|
|
||||||
|
@ -45,8 +44,7 @@ class AI:
|
||||||
|
|
||||||
stream_response = client.chat.completions.create(
|
stream_response = client.chat.completions.create(
|
||||||
model=model,
|
model=model,
|
||||||
messages=messages,
|
messages=messages
|
||||||
stream=True
|
|
||||||
)
|
)
|
||||||
|
|
||||||
with return_class.ai_response_lock:
|
with return_class.ai_response_lock:
|
||||||
|
@ -55,20 +53,3 @@ class AI:
|
||||||
for chunk in stream_response:
|
for chunk in stream_response:
|
||||||
with return_class.ai_response_lock:
|
with return_class.ai_response_lock:
|
||||||
return_class.ai_response[access_token] += chunk.choices[0].delta.content
|
return_class.ai_response[access_token] += chunk.choices[0].delta.content
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def process_anthropic(model, messages, return_class, access_token, api_key):
|
|
||||||
|
|
||||||
client = anthropic.Anthropic(api_key=api_key)
|
|
||||||
|
|
||||||
with return_class.ai_response_lock:
|
|
||||||
return_class.ai_response[access_token] = ""
|
|
||||||
|
|
||||||
with client.messages.stream(
|
|
||||||
max_tokens=1024,
|
|
||||||
model=model,
|
|
||||||
messages=messages,
|
|
||||||
) as stream:
|
|
||||||
for text in stream.text_stream:
|
|
||||||
with return_class.ai_response_lock:
|
|
||||||
return_class.ai_response[access_token] += text
|
|
||||||
|
|
12
py/api.py
12
py/api.py
|
@ -46,18 +46,6 @@ class API:
|
||||||
thread.start()
|
thread.start()
|
||||||
thread.join()
|
thread.join()
|
||||||
return jsonify({'status': 200})
|
return jsonify({'status': 200})
|
||||||
elif model_type == "openai":
|
|
||||||
api_key = data.get('api_key')
|
|
||||||
thread = threading.Thread(target=self.ai.process_openai, args=(ai_model, messages, self, access_token, api_key))
|
|
||||||
thread.start()
|
|
||||||
thread.join()
|
|
||||||
return jsonify({'status': 200})
|
|
||||||
elif model_type == "anthropic":
|
|
||||||
api_key = data.get('api_key')
|
|
||||||
thread = threading.Thread(target=self.ai.process_anthropic, args=(ai_model, messages, self, access_token, api_key))
|
|
||||||
thread.start()
|
|
||||||
thread.join()
|
|
||||||
return jsonify({'status': 200})
|
|
||||||
|
|
||||||
return jsonify({'status': 401, 'error': 'Invalid AI model type'})
|
return jsonify({'status': 401, 'error': 'Invalid AI model type'})
|
||||||
|
|
||||||
|
|
|
@ -3,5 +3,4 @@ flask-cors
|
||||||
ollama
|
ollama
|
||||||
mistralai
|
mistralai
|
||||||
openai
|
openai
|
||||||
anthropic
|
|
||||||
pyOpenSSL
|
pyOpenSSL
|
Loading…
Add table
Add a link
Reference in a new issue