Skip to content

Commit

Permalink
tiktoken
Browse files Browse the repository at this point in the history
  • Loading branch information
efriis committed Dec 7, 2023
1 parent 84025a8 commit f3e61d8
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion libs/langchain/langchain/utilities/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from typing import TYPE_CHECKING, Any, Dict, List, Optional

import requests
import tiktoken
from langchain_core.pydantic_v1 import BaseModel, Extra, root_validator

from langchain.utils import get_from_dict_or_env
Expand All @@ -15,6 +14,18 @@
from github.PullRequest import PullRequest


def _import_tiktoken() -> Any:
"""Import tiktoken."""
try:
import tiktoken
except ImportError:
raise ImportError(
"tiktoken is not installed. "
"Please install it with `pip install tiktoken`"
)
return tiktoken


class GitHubAPIWrapper(BaseModel):
"""Wrapper for GitHub API."""

Expand Down Expand Up @@ -385,6 +396,7 @@ def list_pull_request_files(self, pr_number: int) -> List[Dict[str, Any]]:
dict: A dictionary containing the issue's title,
body, and comments as a string
"""
tiktoken = _import_tiktoken()
MAX_TOKENS_FOR_FILES = 3_000
pr_files = []
pr = self.github_repo_instance.get_pull(number=int(pr_number))
Expand Down Expand Up @@ -453,6 +465,7 @@ def get_pull_request(self, pr_number: int) -> Dict[str, Any]:
total_tokens = 0

def get_tokens(text: str) -> int:
tiktoken = _import_tiktoken()
return len(tiktoken.get_encoding("cl100k_base").encode(text))

def add_to_dict(data_dict: Dict[str, Any], key: str, value: str) -> None:
Expand Down

0 comments on commit f3e61d8

Please sign in to comment.