-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
1,149 additions
and
1,016 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
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# -*- coding: utf-8 -*- | ||
from typing import Any, Dict, Optional, Type | ||
import tiktoken | ||
from langchain_experimental.tools import PythonREPLTool | ||
from langchain.callbacks.manager import ( | ||
AsyncCallbackManagerForToolRun, | ||
CallbackManagerForToolRun, | ||
) | ||
|
||
class PythonTool(PythonREPLTool): | ||
|
||
max_tokens = 2000 | ||
model = "gpt-4" | ||
|
||
def trunk_tokens(self, msg): | ||
# TODO: workarounds for the following context length error with ChatGPT | ||
# https://github.com/hwchase17/langchain/issues/2140 | ||
# https://github.com/hwchase17/langchain/issues/1767 | ||
tokens = tiktoken.encoding_for_model(self.model).encode(msg) | ||
while len(tokens) > self.max_tokens: | ||
msg = msg[:len(msg) // 2] | ||
tokens = tiktoken.encoding_for_model(self.model).encode(msg) | ||
return msg | ||
|
||
def _run( | ||
self, | ||
query: str, | ||
run_manager: Optional[CallbackManagerForToolRun] = None, | ||
) -> Any: | ||
result = super()._run(query, run_manager) | ||
return self.trunk_tokens(result) | ||
|
||
async def _arun( | ||
self, | ||
query: str, | ||
run_manager: Optional[AsyncCallbackManagerForToolRun] = None, | ||
) -> Any: | ||
result = await super()._arun(query, run_manager) | ||
return self.trunk_tokens(result) |
Oops, something went wrong.