File size: 2,102 Bytes
d6c416b 48e31b6 3e312b7 dd34b85 33af6ab 6910501 3a423b8 d6c416b 46dd2a1 6910501 d6c416b 6910501 d6c416b 6910501 3a423b8 6910501 d6c416b 3e312b7 33af6ab 3e312b7 33af6ab 3e312b7 33af6ab 3e312b7 d6c416b 3a423b8 3e312b7 d6c416b 3a423b8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
from openai import OpenAI
from params import OPENAI_MODEL, OPENAI_API_KEY
import llamanet
# Initialize LlamaNet
llamanet.run("start", "https://huggingface.co/arcee-ai/Arcee-Spark-GGUF/blob/main/Arcee-Spark-IQ4_XS.gguf")
# Create an instance of the OpenAI class
client = OpenAI(api_key=OPENAI_API_KEY)
def send_to_chatgpt(msg_list):
try:
completion = client.chat.completions.create(
model=OPENAI_MODEL,
messages=msg_list,
temperature=0.6,
stream=True
)
chatgpt_response = ""
for chunk in completion:
if chunk.choices[0].delta.content is not None:
chatgpt_response += chunk.choices[0].delta.content
# Note: Usage information might not be available with LlamaNet
chatgpt_usage = None
return chatgpt_response, chatgpt_usage
except Exception as e:
print(f"Error in send_to_chatgpt: {str(e)}")
return f"Error: {str(e)}", None
def send_to_llamanet(msg_list):
try:
# Create a new OpenAI client for LlamaNet (no API key needed)
llamanet_client = OpenAI()
# Send request to LlamaNet
completion = llamanet_client.chat.completions.create(
model="gpt-3.5-turbo", # LlamaNet uses this as a placeholder
messages=msg_list,
stream=True
)
llamanet_response = ""
for chunk in completion:
if chunk.choices[0].delta.content is not None:
llamanet_response += chunk.choices[0].delta.content
# LlamaNet doesn't provide usage information
llamanet_usage = None
return llamanet_response, llamanet_usage
except Exception as e:
print(f"Error in send_to_llamanet: {str(e)}")
return f"Error: {str(e)}", None
def send_to_llm(provider, msg_list):
if provider == "llamanet":
return send_to_llamanet(msg_list)
elif provider == "openai":
return send_to_chatgpt(msg_list)
else:
raise ValueError(f"Unknown provider: {provider}")
|