Guest User

koboldcpp simple api terminal bot

a guest
Jul 24th, 2023
4,460
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. import requests, json, os, io, re, base64, random
  2. ENDPOINT = "http://127.0.0.1:5001"
  3. def split_text(text):
  4. parts = re.split(r'\n[a-zA-Z]', text)
  5. return parts
  6. username = "User"
  7. botname = "Assistant"
  8. def get_prompt(conversation_history, username, text): # For KoboldAI Generation
  9. return {
  10. "prompt": conversation_history + f"{username}: {text}\n{botname}:",
  11. "use_story": False,
  12. "use_memory": True,
  13. "use_authors_note": False,
  14. "use_world_info": False,
  15. "max_context_length": 2048,
  16. "max_length": 120,
  17. "rep_pen": 1.0,
  18. "rep_pen_range": 2048,
  19. "rep_pen_slope": 0.7,
  20. "temperature": 0.8,
  21. "tfs": 0.97,
  22. "top_a": 0.8,
  23. "top_k": 0,
  24. "top_p": 0.5,
  25. "typical": 0.19,
  26. "sampler_order": [6, 0, 1, 3, 4, 2, 5],
  27. "singleline": False,
  28. #"sampler_seed": 69420, #set the seed
  29. #"sampler_full_determinism": False, #set it so the seed determines generation content
  30. "frmttriminc": False,
  31. "frmtrmblln": False
  32. }
  33. num_lines_to_keep = 20
  34. global conversation_history
  35. with open(f'conv_history_{botname}_terminal.txt', 'a+') as file:
  36. file.seek(0)
  37. chathistory = file.read()
  38. print(chathistory)
  39. conversation_history = f"{chathistory}"
  40. def handle_message(user_message):
  41. global conversation_history
  42. prompt = get_prompt(conversation_history, username, user_message) # Generate a prompt using the conversation history and user message
  43. response = requests.post(f"{ENDPOINT}/api/v1/generate", json=prompt) # Send the prompt to KoboldAI and get the response
  44. if response.status_code == 200:
  45. results = response.json()['results']
  46. text = results[0]['text'] # Parse the response and get the generated text
  47. response_text = split_text(text)[0]
  48. response_text = response_text.replace(" ", " ")
  49. conversation_history += f"{username}: {user_message}\n{botname}: {response_text}\n" # Update the conversation history with the user message and bot response
  50. with open(f'conv_history_{botname}_terminal.txt', "a") as f:
  51. f.write(f"{username}: {user_message}\n{botname}: {response_text}\n") # Append conversation to text file
  52. response_text = response_text.replace("\n", "")
  53. print(f"{botname}: {response_text}") # Send the response back to the user
  54. while True: # Start the conversation
  55. user_message = input(f"{username}: ") # Get user input from the console
  56. handle_message(user_message)# Handle the user's input and get the bot's response
  57.  
  58.  
  59.  
Comments
Add Comment
Please, Sign In to add comment