NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

import os
from typing import List, Optional, Any, Dict, Generator
from pydantic import Field
from langchain.llms.base import LLM
from gpt4all import GPT4All

class MyGPT4ALL(LLM):
model_folder_path: str = Field(None, alias='model_folder_path')
model_name: str = Field(None, alias='model_name')
allow_download: bool = Field(None, alias='allow_download')
backend: Optional[str] = 'llama'
temp: Optional[float] = 0.7
top_p: Optional[float] = 0.1
top_k: Optional[int] = 40
n_batch: Optional[int] = 8
n_threads: Optional[int] = 4
n_predict: Optional[int] = 256
max_tokens: Optional[int] = 200
repeat_last_n: Optional[int] = 64
repeat_penalty: Optional[float] = 1.18

gpt4_model_instance: Any = None

def __init__(self, model_folder_path, model_name, allow_download, **kwargs):
super(MyGPT4ALL, self).__init__()
self.model_folder_path = model_folder_path
self.model_name = model_name
self.allow_download = allow_download
self.auto_download()

self.gpt4_model_instance = GPT4All(
model_name=self.model_name,
model_path=self.model_folder_path,
)

def auto_download(self):
# Same as your existing auto_download method
pass

@property
def _get_model_default_parameters(self):
return {
"max_tokens": self.max_tokens,
"n_predict": self.n_predict,
"top_k": self.top_k,
"top_p": self.top_p,
"temp": self.temp,
"n_batch": self.n_batch,
"repeat_penalty": self.repeat_penalty,
"repeat_last_n": self.repeat_last_n,
}

@property
def _identifying_params(self) -> Dict[str, Any]:
return {
'model_name': self.model_name,
'model_path': self.model_folder_path,
'model_parameters': self._get_model_default_parameters
}

@property
def _llm_type(self) -> str:
return 'llama'

def _call(self, prompt: str, stop: Optional[List[str]] = None, **kwargs) -> str:
params = {**self._get_model_default_parameters, **kwargs}
response = self.gpt4_model_instance.generate(prompt, **params)
return response

def stream_output(self, prompt: str, **kwargs) -> Generator[str, None, None]:
"""
Stream output from the model token by token.

Args:
prompt: Input prompt for the model.

Yields:
Tokens as they are generated.
"""
params = {**self._get_model_default_parameters, **kwargs}
for token in self.gpt4_model_instance.generate(prompt, stream=True, **params):
yield token

# Chat history management
class ChatHistory:
def __init__(self):
self.history = []

def add_message(self, role: str, message: str):
"""
Add a message to the chat history.

Args:
role: 'user' or 'assistant'
message: The content of the message.
"""
self.history.append({"role": role, "content": message})

def get_conversation(self) -> str:
"""
Return the conversation as a single formatted string.
"""
return "n".join(
f"{entry['role'].capitalize()}: {entry['content']}" for entry in self.history
)

# Example Usage
if __name__ == "__main__":
model_path = "./models"
model_name = "your_model_name.bin"
gpt4_model = MyGPT4ALL(model_path, model_name, allow_download=True)

chat_history = ChatHistory()

# Simulate a conversation
chat_history.add_message("user", "Hello!")
conversation = chat_history.get_conversation()

# Generate response
print("Assistant is typing...")
for token in gpt4_model.stream_output(conversation):
print(token, end="", flush=True)
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.