-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from zoan37/window_dev
Support Window AI
- Loading branch information
Showing
10 changed files
with
218 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -165,6 +165,7 @@ logs/ | |
start.sh | ||
|
||
database.db | ||
database.db-journal | ||
vectors.pickle.gz | ||
agents/*.txt | ||
discord_server.out | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import langchain | ||
from langchain.chat_models.base import BaseChatModel, SimpleChatModel | ||
from langchain.schema import ( | ||
AIMessage, | ||
BaseMessage, | ||
ChatGeneration, | ||
ChatResult, | ||
HumanMessage, | ||
SystemMessage, | ||
) | ||
from typing import Any, Dict, List, Mapping, Optional, Sequence, TypedDict | ||
import websocket | ||
import uuid | ||
import json | ||
|
||
|
||
class MessageDict(TypedDict): | ||
role: str | ||
content: str | ||
|
||
|
||
class RequestDict(TypedDict): | ||
messages: List[MessageDict] | ||
temperature: float | ||
request_id: str | ||
|
||
|
||
class ResponseDict(TypedDict): | ||
content: str | ||
request_id: str | ||
|
||
|
||
class ChatWindowAI(BaseChatModel): | ||
model_name: str = "window" | ||
"""Model name to use.""" | ||
temperature: float = 0 | ||
"""What sampling temperature to use.""" | ||
streaming: bool = False | ||
"""Whether to stream the results.""" | ||
request_timeout: int = 3600 | ||
"""Timeout in seconds for the request.""" | ||
|
||
@property | ||
def _llm_type(self) -> str: | ||
"""Return type of chat model.""" | ||
return "window-chat" | ||
|
||
def _generate( | ||
self, messages: List[BaseMessage], stop: Optional[List[str]] = None | ||
) -> ChatResult: | ||
output_str = self._call(messages, stop=stop) | ||
message = AIMessage(content=output_str) | ||
generation = ChatGeneration(message=message) | ||
result = ChatResult(generations=[generation]) | ||
return result | ||
|
||
async def _agenerate( | ||
self, messages: List[BaseMessage], stop: Optional[List[str]] = None | ||
) -> ChatResult: | ||
return self._generate(messages, stop=stop) | ||
|
||
def _call( | ||
self, messages: List[BaseMessage], stop: Optional[List[str]] = None | ||
) -> str: | ||
request_id = str(uuid.uuid4()) | ||
request: RequestDict = { | ||
"messages": [], | ||
"temperature": self.temperature, | ||
"request_id": request_id, | ||
} | ||
|
||
for message in messages: | ||
role = "user" # default role is user | ||
if isinstance(message, HumanMessage): | ||
role = "user" | ||
elif isinstance(message, AIMessage): | ||
role = "assistant" | ||
elif isinstance(message, SystemMessage): | ||
role = "system" | ||
|
||
request["messages"].append( | ||
{ | ||
"role": role, | ||
"content": message.content, | ||
} | ||
) | ||
|
||
ws = websocket.WebSocket() | ||
ws.connect("ws://127.0.0.1:5000/windowmodel") | ||
ws.send(json.dumps(request)) | ||
message = ws.recv() | ||
ws.close() | ||
|
||
response: ResponseDict = json.loads(message) | ||
|
||
response_content = response["content"] | ||
response_request_id = response["request_id"] | ||
|
||
# sanity check that response corresponds to request | ||
if request_id != response_request_id: | ||
raise ValueError( | ||
f"Invalid request ID: {response_request_id}, expected: {request_id}" | ||
) | ||
|
||
return response_content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters