Skip to content

Commit bcf5986

Browse files
authored
Merge pull request #142 from devchat-ai/eliminate_binary_dependency
eliminate the dependency on binary
2 parents b26be22 + d40ad95 commit bcf5986

10 files changed

Lines changed: 187 additions & 414 deletions

File tree

.circleci/config.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
version: 2.1
22

3-
orbs:
4-
python: circleci/python@2.1.1
5-
63
jobs:
74
build:
8-
executor: python/default
5+
docker:
6+
- image: circleci/python:3.9
97
steps:
108
- checkout
11-
- python/install-packages:
12-
pkg-manager: poetry
139
- run:
14-
name: Install dependencies
10+
name: Install Poetry
11+
command: |
12+
curl -sSL https://install.python-poetry.org | python3 -
13+
- run:
14+
name: Setup Python Environment
1515
command: |
1616
poetry install
1717
- run:
@@ -27,4 +27,4 @@ workflows:
2727
version: 2
2828
build:
2929
jobs:
30-
- build
30+
- build

devchat/__main__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import sys
2+
3+
if __name__ == "__main__":
4+
from devchat._cli.main import main as _main
5+
6+
sys.exit(_main())

devchat/assistant.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import time
33
from typing import Optional, List, Iterator
4+
import openai
45
from devchat.message import Message
56
from devchat.chat import Chat
67
from devchat.store import Store
@@ -95,6 +96,13 @@ def iterate_response(self) -> Iterator[str]:
9596
created_time = int(time.time())
9697
config_params = self._chat.config.dict(exclude_unset=True)
9798
for chunk in self._chat.stream_response(self._prompt):
99+
if isinstance(chunk, openai.types.chat.chat_completion_chunk.ChatCompletionChunk):
100+
chunk = chunk.dict()
101+
if "function_call" in chunk["choices"][0]["delta"] and \
102+
not chunk["choices"][0]["delta"]["function_call"]:
103+
del chunk["choices"][0]["delta"]["function_call"]
104+
if not chunk["choices"][0]["delta"]["content"]:
105+
chunk["choices"][0]["delta"]["content"] = ""
98106
if "id" not in chunk or "index" not in chunk["choices"][0]:
99107
chunk["id"] = "chatcmpl-7vdfQI02x-" + str(created_time)
100108
chunk["object"] = "chat.completion.chunk"

devchat/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sys
44
from typing import List, Dict, Tuple, Union, Optional
55
from pydantic import BaseModel
6-
import yaml
6+
import oyaml as yaml
77
from devchat.openai import OpenAIChatParameters
88
from devchat.anthropic import AnthropicChatParameters
99

devchat/engine/command_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import List, Dict, Optional
2-
import yaml
2+
import oyaml as yaml
33
from pydantic import BaseModel
44
from .namespace import Namespace
55

devchat/openai/openai_chat.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import json
2+
import os
13
from typing import Optional, Union, List, Dict, Iterator
24
from pydantic import BaseModel, Field
35
import openai
@@ -66,10 +68,17 @@ def complete_response(self, prompt: OpenAIPrompt) -> str:
6668
config_params['function_call'] = 'auto'
6769
config_params['stream'] = False
6870

69-
response = openai.ChatCompletion.create(
71+
client = openai.OpenAI(
72+
api_key=os.environ.get("OPENAI_API_KEY", None),
73+
base_url=os.environ.get("OPENAI_API_BASE", None)
74+
)
75+
76+
response = client.chat.completions.create(
7077
messages=prompt.messages,
7178
**config_params
7279
)
80+
if isinstance(response, openai.types.chat.chat_completion.ChatCompletion):
81+
return json.dumps(response.dict())
7382
return str(response)
7483

7584
def stream_response(self, prompt: OpenAIPrompt) -> Iterator:
@@ -80,8 +89,14 @@ def stream_response(self, prompt: OpenAIPrompt) -> Iterator:
8089
config_params['function_call'] = 'auto'
8190
config_params['stream'] = True
8291

83-
response = openai.ChatCompletion.create(
92+
client = openai.OpenAI(
93+
api_key=os.environ.get("OPENAI_API_KEY", None),
94+
base_url=os.environ.get("OPENAI_API_BASE", None)
95+
)
96+
97+
response = client.chat.completions.create(
8498
messages=prompt.messages,
85-
**config_params
99+
**config_params,
100+
timeout=8
86101
)
87102
return response

devchat/openai/openai_prompt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ def append_response(self, delta_str: str) -> str:
214214
self.responses[index].stream_from_dict(delta)
215215

216216
if 'function_call' in delta:
217-
if 'name' in delta['function_call']:
217+
if 'name' in delta['function_call'] and \
218+
self.responses[index].function_call.get('name', '') == '':
218219
self.responses[index].function_call['name'] = \
219-
self.responses[index].function_call.get('name', '') + \
220220
delta['function_call']['name']
221221
if 'arguments' in delta['function_call']:
222222
self.responses[index].function_call['arguments'] = \

devchat/utils.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,20 @@
99
import hashlib
1010
import tiktoken
1111

12+
try:
13+
encoding = tiktoken.get_encoding("cl100k_base")
14+
except Exception:
15+
from tiktoken import registry
16+
from tiktoken.registry import _find_constructors
17+
from tiktoken.core import Encoding
18+
19+
def get_encoding(name: str):
20+
_find_constructors()
21+
constructor = registry.ENCODING_CONSTRUCTORS[name]
22+
return Encoding(**constructor(), use_pure_python=True)
23+
24+
encoding = get_encoding("cl100k_base")
1225

13-
encoding = tiktoken.get_encoding("cl100k_base")
1426
log_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
1527

1628

0 commit comments

Comments
 (0)