Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
ollama
Commits
648f0974
Commit
648f0974
authored
Aug 14, 2023
by
Bruce MacDonald
Browse files
python example
parent
fc5230df
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
0 deletions
+40
-0
docs/python/examples/client.py
docs/python/examples/client.py
+40
-0
No files found.
docs/python/examples/client.py
0 → 100644
View file @
648f0974
import
json
import
requests
# NOTE: ollama must be running for this to work, start the ollama app or run `ollama serve`
model
=
'llama2'
# TODO: update this for whatever model you wish to use
context
=
[]
# the context stores a conversation history, you can use this to make the model more context aware
def
generate
(
prompt
):
global
context
r
=
requests
.
post
(
'http://localhost:11434/api/generate'
,
json
=
{
'model'
:
model
,
'prompt'
:
prompt
,
'context'
:
context
,
},
stream
=
True
)
r
.
raise_for_status
()
for
line
in
r
.
iter_lines
():
body
=
json
.
loads
(
line
)
response_part
=
body
.
get
(
'response'
,
''
)
# the response streams one token at a time, print that as we recieve it
print
(
response_part
,
end
=
''
,
flush
=
True
)
if
'error'
in
body
:
raise
Exception
(
body
[
'error'
])
if
body
.
get
(
'done'
,
False
):
context
=
body
[
'context'
]
return
def
main
():
while
True
:
user_input
=
input
(
"Enter a prompt: "
)
print
()
generate
(
user_input
)
print
()
if
__name__
==
"__main__"
:
main
()
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment