Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
version: 2.1

orbs:
python: circleci/python@2.1.1

jobs:
build:
executor: python/default
docker:
- image: circleci/python:3.9
steps:
- checkout
- python/install-packages:
pkg-manager: poetry
- run:
name: Install dependencies
name: Install Poetry
command: |
curl -sSL https://install.python-poetry.org | python3 -
- run:
name: Setup Python Environment
command: |
poetry install
- run:
Expand All @@ -27,4 +27,4 @@ workflows:
version: 2
build:
jobs:
- build
- build
6 changes: 6 additions & 0 deletions devchat/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import sys

if __name__ == "__main__":
from devchat._cli.main import main as _main

sys.exit(_main())
8 changes: 8 additions & 0 deletions devchat/assistant.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import time
from typing import Optional, List, Iterator
import openai
from devchat.message import Message
from devchat.chat import Chat
from devchat.store import Store
Expand Down Expand Up @@ -95,6 +96,13 @@ def iterate_response(self) -> Iterator[str]:
created_time = int(time.time())
config_params = self._chat.config.dict(exclude_unset=True)
for chunk in self._chat.stream_response(self._prompt):
if isinstance(chunk, openai.types.chat.chat_completion_chunk.ChatCompletionChunk):
chunk = chunk.dict()
if "function_call" in chunk["choices"][0]["delta"] and \
not chunk["choices"][0]["delta"]["function_call"]:
del chunk["choices"][0]["delta"]["function_call"]
if not chunk["choices"][0]["delta"]["content"]:
chunk["choices"][0]["delta"]["content"] = ""
if "id" not in chunk or "index" not in chunk["choices"][0]:
chunk["id"] = "chatcmpl-7vdfQI02x-" + str(created_time)
chunk["object"] = "chat.completion.chunk"
Expand Down
2 changes: 1 addition & 1 deletion devchat/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys
from typing import List, Dict, Tuple, Union, Optional
from pydantic import BaseModel
import yaml
import oyaml as yaml
from devchat.openai import OpenAIChatParameters
from devchat.anthropic import AnthropicChatParameters

Expand Down
2 changes: 1 addition & 1 deletion devchat/engine/command_parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import List, Dict, Optional
import yaml
import oyaml as yaml
from pydantic import BaseModel
from .namespace import Namespace

Expand Down
21 changes: 18 additions & 3 deletions devchat/openai/openai_chat.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json
import os
from typing import Optional, Union, List, Dict, Iterator
from pydantic import BaseModel, Field
import openai
Expand Down Expand Up @@ -66,10 +68,17 @@ def complete_response(self, prompt: OpenAIPrompt) -> str:
config_params['function_call'] = 'auto'
config_params['stream'] = False

response = openai.ChatCompletion.create(
client = openai.OpenAI(
api_key=os.environ.get("OPENAI_API_KEY", None),
base_url=os.environ.get("OPENAI_API_BASE", None)
)

response = client.chat.completions.create(
messages=prompt.messages,
**config_params
)
if isinstance(response, openai.types.chat.chat_completion.ChatCompletion):
return json.dumps(response.dict())
return str(response)

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

response = openai.ChatCompletion.create(
client = openai.OpenAI(
api_key=os.environ.get("OPENAI_API_KEY", None),
base_url=os.environ.get("OPENAI_API_BASE", None)
)

response = client.chat.completions.create(
messages=prompt.messages,
**config_params
**config_params,
timeout=8
)
return response
4 changes: 2 additions & 2 deletions devchat/openai/openai_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ def append_response(self, delta_str: str) -> str:
self.responses[index].stream_from_dict(delta)

if 'function_call' in delta:
if 'name' in delta['function_call']:
if 'name' in delta['function_call'] and \
self.responses[index].function_call.get('name', '') == '':
self.responses[index].function_call['name'] = \
self.responses[index].function_call.get('name', '') + \
delta['function_call']['name']
if 'arguments' in delta['function_call']:
self.responses[index].function_call['arguments'] = \
Expand Down
14 changes: 13 additions & 1 deletion devchat/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,20 @@
import hashlib
import tiktoken

try:
encoding = tiktoken.get_encoding("cl100k_base")
except Exception:
from tiktoken import registry
from tiktoken.registry import _find_constructors
from tiktoken.core import Encoding

def get_encoding(name: str):
_find_constructors()
constructor = registry.ENCODING_CONSTRUCTORS[name]
return Encoding(**constructor(), use_pure_python=True)

encoding = get_encoding("cl100k_base")

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


Expand Down
Loading