@ -112,12 +112,18 @@ class Standalone:
if self . args . output :
if self . args . output :
with open ( self . args . output , " w " ) as f :
with open ( self . args . output , " w " ) as f :
f . write ( buffer )
f . write ( buffer )
if self . args . session :
from . helper import Session
session = Session ( )
session . save_to_session (
system , user , buffer , self . args . session )
message = await stream . get_final_message ( )
message = await stream . get_final_message ( )
async def claudeChat ( self , system , user , copy = False ) :
async def claudeChat ( self , system , user , copy = False ) :
from anthropic import Anthropic
from anthropic import Anthropic
self . claudeApiKey = os . environ [ " CLAUDE_API_KEY " ]
self . claudeApiKey = os . environ [ " CLAUDE_API_KEY " ]
client = Anthropic ( api_key = self . claudeApiKey )
client = Anthropic ( api_key = self . claudeApiKey )
message = None
message = client . messages . create (
message = client . messages . create (
max_tokens = 4096 ,
max_tokens = 4096 ,
system = system ,
system = system ,
@ -132,6 +138,11 @@ class Standalone:
if self . args . output :
if self . args . output :
with open ( self . args . output , " w " ) as f :
with open ( self . args . output , " w " ) as f :
f . write ( message . content [ 0 ] . text )
f . write ( message . content [ 0 ] . text )
if self . args . session :
from . helper import Session
session = Session ( )
session . save_to_session (
system , user , message . content [ 0 ] . text , self . args . session )
def streamMessage ( self , input_data : str , context = " " , host = ' ' ) :
def streamMessage ( self , input_data : str , context = " " , host = ' ' ) :
""" Stream a message and handle exceptions.
""" Stream a message and handle exceptions.
@ -149,23 +160,38 @@ class Standalone:
wisdomFilePath = os . path . join (
wisdomFilePath = os . path . join (
config_directory , f " patterns/ { self . pattern } /system.md "
config_directory , f " patterns/ { self . pattern } /system.md "
)
)
session_message = " "
if self . args . session :
from . helper import Session
session = Session ( )
session_message = session . read_from_session (
self . args . session )
user = session_message + ' \n ' + input_data
user = input_data
user_message = { " role " : " user " , " content " : f " { input_data } " }
user_message = { " role " : " user " , " content " : f " { input_data } " }
wisdom_File = os . path . join ( current_directory , wisdomFilePath )
wisdom_File = os . path . join ( current_directory , wisdomFilePath )
system = " "
buffer = " "
buffer = " "
system = " "
if self . pattern :
if self . pattern :
try :
try :
with open ( wisdom_File , " r " ) as f :
with open ( wisdom_File , " r " ) as f :
if context :
if context :
system = context + ' \n \n ' + f . read ( )
system = context + ' \n \n ' + f . read ( )
if session_message :
system = session_message + ' \n ' + system
else :
else :
system = f . read ( )
system = f . read ( )
if session_message :
system = session_message + ' \n ' + system
system_message = { " role " : " system " , " content " : system }
system_message = { " role " : " system " , " content " : system }
messages = [ system_message , user_message ]
messages = [ system_message , user_message ]
except FileNotFoundError :
except FileNotFoundError :
print ( " pattern not found " )
print ( " pattern not found " )
return
return
else :
else :
if session_message :
user_message [ ' content ' ] = session_message + \
' \n ' + user_message [ ' content ' ]
if context :
if context :
messages = [
messages = [
{ " role " : " system " , " content " : context } , user_message ]
{ " role " : " system " , " content " : context } , user_message ]
@ -219,6 +245,11 @@ class Standalone:
if self . args . output :
if self . args . output :
with open ( self . args . output , " w " ) as f :
with open ( self . args . output , " w " ) as f :
f . write ( buffer )
f . write ( buffer )
if self . args . session :
from . helper import Session
session = Session ( )
session . save_to_session (
system , user , buffer , self . args . session )
def sendMessage ( self , input_data : str , context = " " , host = ' ' ) :
def sendMessage ( self , input_data : str , context = " " , host = ' ' ) :
""" Send a message using the input data and generate a response.
""" Send a message using the input data and generate a response.
@ -236,14 +267,27 @@ class Standalone:
wisdomFilePath = os . path . join (
wisdomFilePath = os . path . join (
config_directory , f " patterns/ { self . pattern } /system.md "
config_directory , f " patterns/ { self . pattern } /system.md "
)
)
user = input_data
user_message = { " role " : " user " , " content " : f " { input_data } " }
user_message = { " role " : " user " , " content " : f " { input_data } " }
wisdom_File = os . path . join ( current_directory , wisdomFilePath )
wisdom_File = os . path . join ( current_directory , wisdomFilePath )
system = " "
system = " "
session_message = " "
if self . args . session :
from . helper import Session
session = Session ( )
session_message = session . read_from_session (
self . args . session )
if self . pattern :
if self . pattern :
try :
try :
with open ( wisdom_File , " r " ) as f :
with open ( wisdom_File , " r " ) as f :
if context :
if context :
if session_message :
system = session_message + ' \n ' + context + ' \n \n ' + f . read ( )
else :
system = context + ' \n \n ' + f . read ( )
system = context + ' \n \n ' + f . read ( )
else :
if session_message :
system = session_message + ' \n ' + f . read ( )
else :
else :
system = f . read ( )
system = f . read ( )
system_message = { " role " : " system " , " content " : system }
system_message = { " role " : " system " , " content " : system }
@ -252,6 +296,9 @@ class Standalone:
print ( " pattern not found " )
print ( " pattern not found " )
return
return
else :
else :
if session_message :
user_message [ ' content ' ] = session_message + \
' \n ' + user_message [ ' content ' ]
if context :
if context :
messages = [
messages = [
{ ' role ' : ' system ' , ' content ' : context } , user_message ]
{ ' role ' : ' system ' , ' content ' : context } , user_message ]
@ -280,6 +327,11 @@ class Standalone:
if self . args . output :
if self . args . output :
with open ( self . args . output , " w " ) as f :
with open ( self . args . output , " w " ) as f :
f . write ( response . choices [ 0 ] . message . content )
f . write ( response . choices [ 0 ] . message . content )
if self . args . session :
from . helper import Session
session = Session ( )
session . save_to_session (
system , user , response . choices [ 0 ] , self . args . session )
except Exception as e :
except Exception as e :
if " All connection attempts failed " in str ( e ) :
if " All connection attempts failed " in str ( e ) :
print (
print (