Skip to content

Commit 736c4c7

Browse files
author
Anmol Singh
committed
setup starcoderchat
0 parents  commit 736c4c7

9 files changed

+161
-0
lines changed

.flake8

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[flake8]
2+
max-line-length = 100
3+
per-file-ignores =
4+
main.py: F401

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
__pycache__/

.pre-commit-config.yaml

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
repos:
2+
# Order matters because pyupgrade may make changes that pycln and black have to clean up
3+
- repo: https://github.com/asottile/pyupgrade
4+
rev: v3.10.1
5+
hooks:
6+
- id: pyupgrade
7+
args: [--py37-plus]
8+
9+
- repo: https://github.com/hadialqattan/pycln
10+
rev: v2.2.0
11+
hooks:
12+
- id: pycln
13+
args: [--config=pyproject.toml]
14+
15+
- repo: https://github.com/pycqa/isort
16+
rev: 5.12.0
17+
hooks:
18+
- id: isort
19+
name: isort (python)
20+
21+
- repo: https://github.com/psf/black
22+
rev: 23.7.0
23+
hooks:
24+
- id: black
25+
language_version: python3.11
26+
27+
- repo: https://github.com/pre-commit/pre-commit-hooks
28+
rev: v4.4.0
29+
hooks:
30+
- id: end-of-file-fixer
31+
- id: trailing-whitespace
32+
33+
- repo: https://github.com/pycqa/flake8
34+
rev: 6.1.0
35+
hooks:
36+
- id: flake8
37+
additional_dependencies:
38+
- flake8-bugbear
39+
- flake8-comprehensions
40+
- flake8-simplify
41+
42+
- repo: https://github.com/pre-commit/mirrors-prettier
43+
rev: v3.0.0
44+
hooks:
45+
- id: prettier

.prettierrc.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
proseWrap: always
2+
printWidth: 88
3+
endOfLine: auto

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Source code for the bot hosted at: https://poe.com/StarCoderChat
2+
3+
This is an an implementation of StarCoderChat model using the
4+
[Poe-Protocol](https://developer.poe.com/api-bots/poe-protocol-specification). In order
5+
to run this yourself, [setup modal](https://modal.com/docs/guide#getting-started), setup
6+
a secret on modal.com with your Poe security key and your Together API Key, rename the
7+
secret in main.py and then run `modal serve main.py`. The command will deploy this app
8+
and output a url. Use that url to create a poe bot at: https://poe.com/create_bot?api=1

main.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import os
2+
3+
from fastapi_poe import make_app
4+
from modal import Image, Secret, Stub, asgi_app
5+
6+
from star_coder_chat import StarCoderChatBot
7+
8+
image = Image.debian_slim().pip_install_from_requirements("requirements.txt")
9+
stub = Stub("star-coder-chat-app")
10+
11+
12+
@stub.function(image=image, secret=Secret.from_name("star-coder-chat-secret"))
13+
@asgi_app()
14+
def fastapi_app():
15+
bot = StarCoderChatBot(TOGETHER_API_KEY=os.environ["TOGETHER_API_KEY"])
16+
app = make_app(bot, api_key=os.environ["POE_API_KEY"])
17+
return app

pyproject.toml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[tool.black]
2+
target-version = ['py37']
3+
skip-magic-trailing-comma = true
4+
5+
[tool.pycln]
6+
all = true
7+
exclude = "main\\.py"
8+
9+
[tool.isort]
10+
profile = "black"
11+
combine_as_imports = true
12+
skip_gitignore = true

requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
fastapi-poe==0.0.14
2+
httpx-sse==0.3.1

star_coder_chat.py

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from __future__ import annotations
2+
3+
import json
4+
from dataclasses import dataclass
5+
from typing import AsyncIterable
6+
7+
import httpx
8+
import httpx_sse
9+
from fastapi_poe import PoeBot
10+
from fastapi_poe.types import QueryRequest
11+
from sse_starlette.sse import ServerSentEvent
12+
13+
BASE_URL = "https://api.together.xyz/inference"
14+
DEFAULT_SYSTEM_PROMPT = """\
15+
You are the StarCoderChat bot. You help users with programming and code related questions.
16+
Wrap any code blocks in your response in backticks so that it can be rendered using Markdown."""
17+
18+
19+
@dataclass
20+
class StarCoderChatBot(PoeBot):
21+
TOGETHER_API_KEY: str # Together.ai api key
22+
23+
def construct_prompt(self, query: QueryRequest):
24+
prompt = "\n"
25+
prompt += f"<system>: {DEFAULT_SYSTEM_PROMPT}\n"
26+
for message in query.query:
27+
if message.role == "user":
28+
prompt += f"<human>: {message.content}\n"
29+
elif message.role == "bot":
30+
prompt += f"<bot>: {message.content}\n"
31+
elif message.role == "system":
32+
pass
33+
else:
34+
raise ValueError(f"unknown role {message.role}.")
35+
prompt += "<bot>:"
36+
return prompt
37+
38+
async def query_together_ai(self, prompt) -> str:
39+
payload = {
40+
"model": "HuggingFaceH4/starchat-alpha",
41+
"prompt": prompt,
42+
"max_tokens": 1000,
43+
"stop": ["<|endoftext|>", "<|end|>", "<human>", "<bot>"],
44+
"stream_tokens": True,
45+
"temperature": 0.7,
46+
"top_p": 0.7,
47+
"top_k": 50,
48+
"repetition_penalty": 1,
49+
}
50+
headers = {
51+
"accept": "application/json",
52+
"content-type": "application/json",
53+
"Authorization": f"Bearer {self.TOGETHER_API_KEY}",
54+
}
55+
56+
async with httpx.AsyncClient() as aclient:
57+
async with httpx_sse.aconnect_sse(
58+
aclient, "POST", BASE_URL, headers=headers, json=payload
59+
) as event_source:
60+
async for event in event_source.aiter_sse():
61+
if event.data != "[DONE]":
62+
token = json.loads(event.data)["choices"][0]["text"]
63+
yield token
64+
65+
async def get_response(self, query: QueryRequest) -> AsyncIterable[ServerSentEvent]:
66+
prompt = self.construct_prompt(query)
67+
async for word in self.query_together_ai(prompt):
68+
yield self.text_event(word)

0 commit comments

Comments
 (0)