forked from pathwaycom/llm-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
66 lines (49 loc) · 1.5 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
REST Microservice implementing a simple, contextless Chatbot.
The program responds to each query by directly forwarding it to the OpenAI API.
Usage:
In the root of this repository run:
`poetry run ./run_examples.py contextless`
or, if all dependencies are managed manually rather than using poetry
`python examples/pipelines/contextless/app.py`
You can also run this example directly in the environment with llm_app installed.
To call the REST API:
curl --data '{"user": "user", "query": "How to connect to Kafka in Pathway?"}' http://localhost:8080/ | jq
"""
import os
import pathway as pw
from llm_app.model_wrappers import OpenAIChatGPTModel
class QueryInputSchema(pw.Schema):
query: str
user: str
def run(
*,
api_key: str = os.environ.get("OPENAI_API_KEY", ""),
host: str = "0.0.0.0",
port: int = 8080,
model_locator: str = "gpt-3.5-turbo",
max_tokens: int = 60,
temperature: float = 0.8,
**kwargs,
):
query, response_writer = pw.io.http.rest_connector(
host=host,
port=port,
schema=QueryInputSchema,
autocommit_duration_ms=50,
delete_completed_queries=True,
)
model = OpenAIChatGPTModel(api_key=api_key)
responses = query.select(
query_id=pw.this.id,
result=model.apply(
pw.this.query,
locator=model_locator,
temperature=temperature,
max_tokens=max_tokens,
),
)
response_writer(responses)
pw.run()
if __name__ == "__main__":
run()